{
    "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "factoryName": {
            "type": "string",
            "metadata": "Data Factory name"
        },
        "CompanyName": {
            "type" : "string"
        },
        "TablePrefix": {
            "type" : "String"
        },
        "PipelineStartDate": {
            "type": "String"
        },
        "PipelineStartTime": {
            "type": "String",
            "allowedValues": [ "00","01","02","03","04","05","06","07","08","09","10","11","12","13","14","15","16","17","18","19","20","21","22","23" ]
        },
        "LSInsightServerName": {
            "type": "String"
        },
        "LSInsightDatabaseName": {
            "type": "String"
        },
        "UserName": {
            "type": "String"
        },
        "Password": {
            "type": "String"
        },
        "ServerName": {
            "type": "String"
        },
        "DatabaseName": {
            "type": "String"
        },
        "SqlLogin": {
            "type": "String"
        },
        "SqlPassword": {
            "type": "String"
        },
        "LSInsightLinkedServiceType": {
            "type": "String",
            "allowedValues": [ "SqlServer","AzureSqlDatabase" ]
        },
        "LSCentralLinkedServiceType": {
            "type": "String",
            "allowedValues": [ "SqlServer","AzureSqlDatabase" ]
        }
    },
    "variables": {
        "factoryId": "[concat('Microsoft.DataFactory/factories/', parameters('factoryName'))]"
    },
    "resources": [
        {
            "name": "[concat(parameters('factoryName'), '/LSInsight-integrationRuntime')]",
            "type": "Microsoft.DataFactory/factories/integrationRuntimes",
            "apiVersion": "2018-06-01",
            "properties": {
                "type": "SelfHosted",
                "description": "The Integration Runtime (IR) is the compute infrastructure used by Azure Data Factory to copy data across data stores in public network and data stores in private network (on-premises or virtual private network). It provides support for built-in connectors, format conversion, column mapping, and performant and scalable data transfer",
                "typeProperties": {}
            },
            "dependsOn": []
        },
        {
            "name": "[concat(parameters('factoryName'), '/Daily Scheduled Run')]",
            "type": "Microsoft.DataFactory/factories/triggers",
            "apiVersion": "2018-06-01",
            "properties": {
                "annotations": [],
                "runtimeState": "Started",
                "pipelines": [
                    {
                        "pipelineReference": {
                            "referenceName": "Scheduled Run",
                            "type": "PipelineReference"
                        },
                        "parameters": {}
                    }
                ],
                "type": "ScheduleTrigger",
                "typeProperties": {
                    "recurrence": {
                        "frequency": "Hour",
                        "interval": 24,
                        "startTime": "[concat(parameters('PipelineStartDate'),'T',parameters('PipelineStartTime'),':00:00Z')]",
                        "timeZone": "UTC"
                    }
                }
            },
            "dependsOn": [
                "[concat(variables('factoryId'), '/pipelines/Scheduled Run')]"
            ]
        },
        {
            "name": "[concat(parameters('factoryName'), '/LSInsightDW')]",
            "type": "Microsoft.DataFactory/factories/linkedServices",
            "apiVersion": "2018-06-01",
            "properties": {
                "description": "LS Insight Data Warehouse",
                "parameters": {
                    "LSInsightServerName": {
                        "type": "string",
                        "defaultValue": "[parameters('LSInsightServerName')]"
                    },
                    "LSInsightDatabaseName": {
                        "type": "string",
                        "defaultValue": "[parameters('LSInsightDatabaseName')]"
                    },
                    "UserName": {
                        "type": "string",
                        "defaultValue": "[parameters('UserName')]"
                    },
                    "Password": {
                        "type": "string",
                        "defaultValue": "[parameters('Password')]"
                    }
                },
                "annotations": [],
                "type": "[parameters('LSInsightLinkedServiceType')]",
                "typeProperties": {
                    "connectionString": "Integrated Security=False;Connection Timeout=30;Data Source=@{linkedService().LSInsightServerName};Initial Catalog=@{linkedService().LSInsightDatabaseName};User ID=@{linkedService().UserName};Password=@{linkedService().Password}"
                },
                "connectVia": "[if(equals(parameters('LSInsightLinkedServiceType'), 'SqlServer'), json('{\"referenceName\": \"LSInsight-integrationRuntime\", \"type\": \"IntegrationRuntimeReference\"}'), json('null'))]" 
            },
            "dependsOn": [
                "[concat(variables('factoryId'), '/integrationRuntimes/LSInsight-integrationRuntime')]"
            ]
        },
        {
            "name": "[concat(parameters('factoryName'), '/LSCentralSource')]",
            "type": "Microsoft.DataFactory/factories/linkedServices",
            "apiVersion": "2018-06-01",
            "properties": {
                    "description": "LS Central on Prem Database",
                    "parameters": {
                        "ServerName": {
                            "type": "string",
                            "defaultValue": "[parameters('ServerName')]"
                        },
                        "DatabaseName": {
                            "type": "string",
                            "defaultValue": "[parameters('DatabaseName')]"
                        },
                        "SqlLogin": {
                            "type": "string",
                            "defaultValue": "[parameters('SqlLogin')]"
                        },
                        "SqlPassword": {
                            "type": "string",
                            "defaultValue": "[parameters('SqlPassword')]"
                        }
                    },
                "annotations": [],
                "type": "[parameters('LSCentralLinkedServiceType')]",
                "typeProperties": {
                    "connectionString": "Integrated Security=False;Data Source=@{linkedService().ServerName};Initial Catalog=@{linkedService().DatabaseName};User ID=@{linkedService().SqlLogin};Password=@{linkedService().SqlPassword}"
                },
                "connectVia": "[if(equals(parameters('LSCentralLinkedServiceType'), 'SqlServer'), json('{\"referenceName\": \"LSInsight-integrationRuntime\", \"type\": \"IntegrationRuntimeReference\"}'), json('null'))]" 
            },
            "dependsOn": [
                "[concat(variables('factoryId'), '/integrationRuntimes/LSInsight-integrationRuntime')]"
            ]
        },
        {
           "name": "[concat(parameters('factoryName'), '/LSCentralColumnMetadata')]",
            "type": "Microsoft.DataFactory/factories/datasets",
            "apiVersion": "2018-06-01",
            "properties": {
                "linkedServiceName": {
                    "referenceName": "LSInsightDW",
                    "type": "LinkedServiceReference"
                },
                "folder": {
                    "name": "LS Insight Datasets"
                },
                "annotations": [],
                "type": "AzureSqlTable",
                "schema": [
                                {
                                    "name": "TABLE_NAME",
                                    "type": "nvarchar"
                                },
                                {
                                    "name": "COLUMN_NAME",
                                    "type": "nvarchar"
                                },
                                {
                                    "name": "DATA_TYPE",
                                    "type": "nvarchar"
                                },
                                {
                                    "name": "ORDINAL_POSITION",
                                    "type": "int",
                                    "precision": 10
                                },
                                {
                                    "name": "CHARACTER_MAXIMUM_LENGTH",
                                    "type": "int",
                                    "precision": 10
                                },
                                {
                                    "name": "NUMERIC_PRECISION",
                                    "type": "tinyint",
                                    "precision": 3
                                },
                                {
                                    "name": "NUMERIC_SCALE",
                                    "type": "int",
                                    "precision": 10
                                },
                                {
                                    "name": "CountRows",
                                    "type": "bigint",
                                    "precision": 19
                                },
                                {
                                    "name": "BaseTableName",
                                    "type": "nvarchar"
                                },
                                {
                                    "name": "TablePrefix",
                                    "type": "nvarchar"
                                },
                                {
                                    "name": "ExtensionGUID",
                                    "type": "nvarchar"
                                },
                                {
                                    "name": "Keycol",
                                    "type": "varchar"
                                }
                            ],
                "typeProperties": {
                    "schema": "dbo",
                    "table": "LSInsight$LSCentralColumnMetadata"
                }
            },
            "dependsOn": [
                "[concat(variables('factoryId'), '/linkedServices/LSInsightDW')]"
            ]
        },
        {
            "name": "[concat(parameters('factoryName'), '/LSInsightDW')]",
            "type": "Microsoft.DataFactory/factories/datasets",
            "apiVersion": "2018-06-01",
             "properties": {
                "linkedServiceName": {
                    "referenceName": "LSInsightDW",
                    "type": "LinkedServiceReference"
                },
                "parameters": {
                    "DestTableName": {
                        "type": "string"
                    }
                },
                "folder": {
                    "name": "LS Insight Datasets"
                },
                "annotations": [],
                "type": "AzureSqlTable",
                "schema": [],
                "typeProperties": {
                    "table": {
                        "value": "@dataset().DestTableName",
                        "type": "Expression"
                    }
                }
            },
            "dependsOn": [
                "[concat(variables('factoryId'), '/linkedServices/LSInsightDW')]"
            ]
        },
		{
            "name": "[concat(parameters('factoryName'), '/SourceDataset')]",
            "type": "Microsoft.DataFactory/factories/datasets",
            "apiVersion": "2018-06-01",
             "properties": {
                "description": "Connection to your source data store.",
                "linkedServiceName": {
                    "referenceName": "LSCentralSource",
                    "type": "LinkedServiceReference",
                    "parameters": {
                       
                    }
                },
                "parameters": {
                    "dest_table": {
                        "type": "String"
                    }
                },
                "folder": {
                    "name": "LS Insight Datasets"
                },
                "annotations": [],
                "type": "SqlServerTable",
                "schema": [],
                "typeProperties": {
                    "schema": "dbo",
                    "table": {
                        "value": "@dataset().dest_table",
                        "type": "Expression"
                    }
                }
            },
            "dependsOn": [
                "[concat(variables('factoryId'), '/linkedServices/LSCentralSource')]"
            ]
        },

        {
            "name": "[concat(parameters('factoryName'), '/All staging Tables')]",
            "type": "Microsoft.DataFactory/factories/pipelines",
            "apiVersion": "2018-06-01",
            "properties": {
            "activities": [
            {
                "name": "PipelineStart",
                "type": "SetVariable",
                "dependsOn": [],
                "userProperties": [],
                "typeProperties": {
                    "variableName": "PipelineStart",
                    "value": {
                        "value": "@utcnow()",
                        "type": "Expression"
                    }
                }
            },
            {
                "name": "For Each Table",
                "type": "ForEach",
                "dependsOn": [
                    {
                        "activity": "Append VarQueryBase",
                        "dependencyConditions": [
                            "Succeeded"
                        ]
                    }
                ],
                "userProperties": [],
                "typeProperties": {
                    "items": {
                        "value": "@variables('VarQueryBase1')",
                        "type": "Expression"
                    },
                    "activities": [
                        {
                            "name": "GetLastTimestamp",
                            "description": "Retrieve  the max rowversion value stored in the control table",
                            "type": "Lookup",
                            "dependsOn": [],
                            "policy": {
                                "timeout": "7.00:00:00",
                                "retry": 0,
                                "retryIntervalInSeconds": 30
                            },
                            "userProperties": [],
                            "typeProperties": {
                                "source": {
                                    "type": "AzureSqlSource",
                                    "sqlReaderQuery": {
                                        "value": "@concat('SELECT max([NewRowVersion])  AS LastTimestamp \n  FROM ', variables('Control_Table_Table_Name'), '\n  where [Table] =''',item().BaseTableName,''' AND [CompanyPrefix] = ''',item().TablePrefix, ''' ')",
                                        "type": "Expression"
                                    },
                                    "partitionOption": "None"
                                },
                                "dataset": {
                                    "referenceName": "LSInsightDW",
                                    "type": "DatasetReference",
                                    "parameters": {
                                        "DestTableName": "NA"
                                    }
                                },
                                "firstRowOnly": true
                            }
                        },
                        {
                            "name": "GetCurrentTimestamp",
                            "description": "Retrieve  the current maximum value in watermark column of source data store",
                            "type": "Lookup",
                            "dependsOn": [],
                            "policy": {
                                "timeout": "7.00:00:00",
                                "retry": 0,
                                "retryIntervalInSeconds": 30
                            },
                            "userProperties": [],
                            "typeProperties": {
                                "source": {
                                    "type": "SqlServerSource",
                                    "sqlReaderQuery": {
                                        "value": "@concat('select COALESCE(MAX(',item().IncrementalKey, '),0) as CurrentTimestamp from ', \nitem().TableSnippet , ' ' ,item().JoinSnippet)",
                                        "type": "Expression"
                                    },
                                    "queryTimeout": "02:00:00",
                                    "partitionOption": "None"
                                },
                                "dataset": {
                                    "referenceName": "SourceDataset",
                                    "type": "DatasetReference",
                                    "parameters": {
                                        "dest_table": "NA"
                                    }
                                }
                            }
                        },
                        {
                            "name": "ErrorOnGetCurrentTimestamp",
                            "description": "Stored procedure activity to store the new Pipeline run metadata in LSAudit table",
                            "type": "SqlServerStoredProcedure",
                            "dependsOn": [
                                {
                                    "activity": "GetCurrentTimestamp",
                                    "dependencyConditions": [
                                        "Failed"
                                    ]
                                }
                            ],
                            "policy": {
                                "timeout": "7.00:00:00",
                                "retry": 0,
                                "retryIntervalInSeconds": 30
                            },
                            "userProperties": [],
                            "typeProperties": {
                                "storedProcedureName": "[[dbo].[UpdateLSInsightAudit]",
                                "storedProcedureParameters": {
                                    "ADFRunGUID": {
                                        "value": {
                                            "value": "@pipeline().RunId",
                                            "type": "Expression"
                                        },
                                        "type": "String"
                                    },
                                    "CopyDuration": {
                                        "value": {
                                            "value": "0",
                                            "type": "Expression"
                                        },
                                        "type": "Int32"
                                    },
                                    "PipelineName": {
                                        "value": {
                                            "value": "@concat('Failed Run - ',  pipeline().Pipeline)",
                                            "type": "Expression"
                                        },
                                        "type": "String"
                                    },
                                    "PipelineStart": {
                                        "value": {
                                            "value": "@variables('PipelineStart')",
                                            "type": "Expression"
                                        },
                                        "type": "DateTime"
                                    },
                                    "RowsCopied": {
                                        "value": {
                                            "value": "0",
                                            "type": "Expression"
                                        },
                                        "type": "Int32"
                                    },
                                    "RowsRead": {
                                        "value": {
                                            "value": "0",
                                            "type": "Expression"
                                        },
                                        "type": "Int32"
                                    },
                                    "RowVersion": {
                                        "value": {
                                            "value": "0",
                                            "type": "Expression"
                                        },
                                        "type": "Int64"
                                    },
                                    "Table": {
                                        "value": {
                                            "value": "@item().BaseTableName",
                                            "type": "Expression"
                                        },
                                        "type": "String"
                                    },
                                    "PipelineTriggerType": {
                                        "value": {
                                            "value": "@pipeline().TriggerType",
                                            "type": "Expression"
                                        },
                                        "type": "String"
                                    },
                                    "ErrorMessage": {
                                        "value": {
                                            "value": "@activity('GetCurrentTimestamp').Error.Message",
                                            "type": "Expression"
                                        },
                                        "type": "string"
                                    },
                                    "CompanyPrefix": {
                                        "value": {
                                            "value": "@item().TablePrefix",
                                            "type": "Expression"
                                        },
                                        "type": "String"
                                    }
                                }
                            },
                            "linkedServiceName": {
                                "referenceName": "LSInsightDW",
                                "type": "LinkedServiceReference"
                            }
                        },
                        {
                            "name": "ErrorOnGetLastTimestamp",
                            "description": "Stored procedure activity to store the new Pipeline run metadata in LSAudit table",
                            "type": "SqlServerStoredProcedure",
                            "dependsOn": [
                                {
                                    "activity": "GetLastTimestamp",
                                    "dependencyConditions": [
                                        "Failed"
                                    ]
                                }
                            ],
                            "policy": {
                                "timeout": "7.00:00:00",
                                "retry": 0,
                                "retryIntervalInSeconds": 30
                            },
                            "userProperties": [],
                            "typeProperties": {
                                "storedProcedureName": "[[dbo].[UpdateLSInsightAudit]",
                                "storedProcedureParameters": {
                                    "ADFRunGUID": {
                                        "value": {
                                            "value": "@pipeline().RunId",
                                            "type": "Expression"
                                        },
                                        "type": "String"
                                    },
                                    "CopyDuration": {
                                        "value": {
                                            "value": "0",
                                            "type": "Expression"
                                        },
                                        "type": "Int32"
                                    },
                                    "PipelineName": {
                                        "value": {
                                            "value": "@concat('Failed Run - ',  pipeline().Pipeline)",
                                            "type": "Expression"
                                        },
                                        "type": "String"
                                    },
                                    "PipelineStart": {
                                        "value": {
                                            "value": "@variables('PipelineStart')",
                                            "type": "Expression"
                                        },
                                        "type": "DateTime"
                                    },
                                    "RowsCopied": {
                                        "value": {
                                            "value": "0",
                                            "type": "Expression"
                                        },
                                        "type": "Int32"
                                    },
                                    "RowsRead": {
                                        "value": {
                                            "value": "0",
                                            "type": "Expression"
                                        },
                                        "type": "Int32"
                                    },
                                    "RowVersion": {
                                        "value": {
                                            "value": "0",
                                            "type": "Expression"
                                        },
                                        "type": "Int64"
                                    },
                                    "Table": {
                                        "value": {
                                            "value": "@item().BaseTableName",
                                            "type": "Expression"
                                        },
                                        "type": "String"
                                    },
                                    "PipelineTriggerType": {
                                        "value": {
                                            "value": "@pipeline().TriggerType",
                                            "type": "Expression"
                                        },
                                        "type": "String"
                                    },
                                    "ErrorMessage": {
                                        "value": {
                                            "value": "@activity('GetLastTimestamp').Error.Message",
                                            "type": "Expression"
                                        },
                                        "type": "string"
                                    },
                                    "CompanyPrefix": {
                                        "value": {
                                            "value": "@item().TablePrefix",
                                            "type": "Expression"
                                        },
                                        "type": "String"
                                    }
                                }
                            },
                            "linkedServiceName": {
                                "referenceName": "LSInsightDW",
                                "type": "LinkedServiceReference"
                            }
                        },
                        {
                            "name": "LoadDataToStagingTables",
                            "type": "Copy",
                            "dependsOn": [
                                {
                                    "activity": "GetLastTimestamp",
                                    "dependencyConditions": [
                                        "Succeeded"
                                    ]
                                },
                                {
                                    "activity": "GetCurrentTimestamp",
                                    "dependencyConditions": [
                                        "Succeeded"
                                    ]
                                }
                            ],
                            "policy": {
                                "timeout": "7.00:00:00",
                                "retry": 0,
                                "retryIntervalInSeconds": 30,
                                "secureOutput": false,
                                "secureInput": false
                            },
                            "userProperties": [],
                            "typeProperties": {
                                "source": {
                                    "type": "SqlServerSource",
                                    "additionalColumns": [
                                        {
                                            "name": "CompanyPrefix",
                                            "value": {
                                                "value": "@item().TablePrefix",
                                                "type": "Expression"
                                            }
                                        }
                                    ],
                                    "sqlReaderQuery": {
                                        "value": "@concat(item().SelectStatement, \nif(\nlessOrEquals(item().SumCountRows,pipeline().parameters.FullLoadthreshold),' ',\nconcat(' AND ' , \nitem().IncrementalKey,' >  ',if(equals(activity('GetLastTimestamp').output.firstRow.LastTimestamp,NULL),0,activity('GetLastTimestamp').output.firstRow.LastTimestamp),' AND ', item().IncrementalKey  ,' <= ',if(equals(activity('GetCurrentTimestamp').output.firstRow.CurrentTimestamp,NULL),0,activity('GetCurrentTimestamp').output.firstRow.CurrentTimestamp))))",
                                        "type": "Expression"
                                    },
                                    "queryTimeout": "02:00:00",
                                    "partitionOption": "None"
                                },
                                "sink": {
                                    "type": "AzureSqlSink",
                                    "preCopyScript": {
                                        "value": "@{concat(\n'IF NOT EXISTS (SELECT\n\t\t*\n\tFROM sys.objects\n\tWHERE object_id = OBJECT_ID(''', item().DestinationTable,''')\n\tAND type IN (''U''))\n BEGIN ',\nitem().CreateSnippet\n,\n' END \nELSE \n BEGIN \n ',if(\nlessOrEquals(item().SumCountRows,pipeline().parameters.FullLoadthreshold)\n,item().DropAndCreate \n,concat('DELETE FROM  [' , \nitem().DestinationTable, ']  WHERE bigint_timestamp >  ',if(equals(activity('GetLastTimestamp').output.firstRow.LastTimestamp,NULL),0,activity('GetLastTimestamp').output.firstRow.LastTimestamp),' AND bigint_timestamp <= ',if(equals(activity('GetCurrentTimestamp').output.firstRow.CurrentTimestamp,NULL),0,activity('GetCurrentTimestamp').output.firstRow.CurrentTimestamp)\n,' AND [CompanyPrefix] = ''',item().TablePrefix, ''''\n))\n ,' END' \n)}",
                                        "type": "Expression"
                                    },
                                    "disableMetricsCollection": false
                                },
                                "enableStaging": false,
                                "translator": {
                                    "type": "TabularTranslator",
                                    "typeConversion": true,
                                    "typeConversionSettings": {
                                        "allowDataTruncation": true,
                                        "treatBooleanAsNumber": false
                                    }
                                }
                            },
                            "inputs": [
                                {
                                    "referenceName": "SourceDataset",
                                    "type": "DatasetReference",
                                    "parameters": {
                                        "dest_table": {
                                            "value": "@item().DestinationTable",
                                            "type": "Expression"
                                        }
                                    }
                                }
                            ],
                            "outputs": [
                                {
                                    "referenceName": "LSInsightDW",
                                    "type": "DatasetReference",
                                    "parameters": {
                                        "DestTableName": {
                                            "value": "@item().DestinationTable",
                                            "type": "Expression"
                                        }
                                    }
                                }
                            ]
                        },
                        {
                            "name": "UpdateLSInsightAudit",
                            "description": "Stored procedure activity to store the new Pipeline run metadata in LSAudit table",
                            "type": "SqlServerStoredProcedure",
                            "dependsOn": [
                                {
                                    "activity": "LoadDataToStagingTables",
                                    "dependencyConditions": [
                                        "Succeeded"
                                    ]
                                }
                            ],
                            "policy": {
                                "timeout": "7.00:00:00",
                                "retry": 0,
                                "retryIntervalInSeconds": 30
                            },
                            "userProperties": [],
                            "typeProperties": {
                                "storedProcedureName": "[[dbo].[UpdateLSInsightAudit]",
                                "storedProcedureParameters": {
                                    "ADFRunGUID": {
                                        "value": {
                                            "value": "@pipeline().RunId",
                                            "type": "Expression"
                                        },
                                        "type": "String"
                                    },
                                    "CopyDuration": {
                                        "value": {
                                            "value": "@activity('LoadDataToStagingTables').output.copyDuration",
                                            "type": "Expression"
                                        },
                                        "type": "Int32"
                                    },
                                    "PipelineName": {
                                        "value": {
                                            "value": "@pipeline().Pipeline",
                                            "type": "Expression"
                                        },
                                        "type": "String"
                                    },
                                    "PipelineStart": {
                                        "value": {
                                            "value": "@variables('PipelineStart')",
                                            "type": "Expression"
                                        },
                                        "type": "DateTime"
                                    },
                                    "RowsCopied": {
                                        "value": {
                                            "value": "@activity('LoadDataToStagingTables').output.rowsCopied",
                                            "type": "Expression"
                                        },
                                        "type": "Int32"
                                    },
                                    "RowsRead": {
                                        "value": {
                                            "value": "@activity('LoadDataToStagingTables').output.rowsRead",
                                            "type": "Expression"
                                        },
                                        "type": "Int32"
                                    },
                                    "RowVersion": {
                                        "value": {
                                            "value": "@activity('GetCurrentTimestamp').output.firstRow.CurrentTimestamp",
                                            "type": "Expression"
                                        },
                                        "type": "Int64"
                                    },
                                    "Table": {
                                        "value": {
                                            "value": "@item().BaseTableName",
                                            "type": "Expression"
                                        },
                                        "type": "String"
                                    },
                                    "PipelineTriggerType": {
                                        "value": {
                                            "value": "@pipeline().TriggerType",
                                            "type": "Expression"
                                        },
                                        "type": "String"
                                    },
                                    "ErrorMessage": {
                                        "value": "",
                                        "type": "String"
                                    },
                                    "CompanyPrefix": {
                                        "value": {
                                            "value": "@item().TablePrefix",
                                            "type": "Expression"
                                        },
                                        "type": "String"
                                    },
                                    "Note": {
                                        "value": {
                                            "value": "@if(\nlessOrEquals(item().SumCountRows,pipeline().parameters.FullLoadthreshold),'Full load',concat('Incremental load. source row count estimate: ' , item().SumCountRows))",
                                            "type": "Expression"
                                        },
                                        "type": "String"
                                    }
                                }
                            },
                            "linkedServiceName": {
                                "referenceName": "LSInsightDW",
                                "type": "LinkedServiceReference"
                            }
                        },
                        {
                            "name": "OnCopyError",
                            "description": "Stored procedure activity to store the new Pipeline run metadata in LSAudit table On error",
                            "type": "SqlServerStoredProcedure",
                            "dependsOn": [
                                {
                                    "activity": "LoadDataToStagingTables",
                                    "dependencyConditions": [
                                        "Failed"
                                    ]
                                }
                            ],
                            "policy": {
                                "timeout": "7.00:00:00",
                                "retry": 0,
                                "retryIntervalInSeconds": 30
                            },
                            "userProperties": [],
                            "typeProperties": {
                                "storedProcedureName": "[[dbo].[UpdateLSInsightAudit]",
                                "storedProcedureParameters": {
                                    "ADFRunGUID": {
                                        "value": {
                                            "value": "@pipeline().RunId",
                                            "type": "Expression"
                                        },
                                        "type": "String"
                                    },
                                    "CopyDuration": {
                                        "value": {
                                            "value": "0",
                                            "type": "Expression"
                                        },
                                        "type": "Int32"
                                    },
                                    "PipelineName": {
                                        "value": {
                                            "value": "@concat('Failed Run - ',  pipeline().Pipeline)",
                                            "type": "Expression"
                                        },
                                        "type": "String"
                                    },
                                    "PipelineStart": {
                                        "value": {
                                            "value": "@variables('PipelineStart')",
                                            "type": "Expression"
                                        },
                                        "type": "DateTime"
                                    },
                                    "RowsCopied": {
                                        "value": {
                                            "value": "0",
                                            "type": "Expression"
                                        },
                                        "type": "Int32"
                                    },
                                    "RowsRead": {
                                        "value": {
                                            "value": "0",
                                            "type": "Expression"
                                        },
                                        "type": "Int32"
                                    },
                                    "RowVersion": {
                                        "value": {
                                            "value": "0",
                                            "type": "Expression"
                                        },
                                        "type": "Int64"
                                    },
                                    "Table": {
                                        "value": {
                                            "value": "@item().BaseTableName",
                                            "type": "Expression"
                                        },
                                        "type": "String"
                                    },
                                    "PipelineTriggerType": {
                                        "value": {
                                            "value": "@pipeline().TriggerType",
                                            "type": "Expression"
                                        },
                                        "type": "String"
                                    },
                                    "ErrorMessage": {
                                        "value": {
                                            "value": "@activity('LoadDataToStagingTables').Error.Message",
                                            "type": "Expression"
                                        },
                                        "type": "string"
                                    },
                                    "CompanyPrefix": {
                                        "value": {
                                            "value": "@item().TablePrefix",
                                            "type": "Expression"
                                        },
                                        "type": "String"
                                    }
                                }
                            },
                            "linkedServiceName": {
                                "referenceName": "LSInsightDW",
                                "type": "LinkedServiceReference"
                            }
                        }
                    ]
                }
            },
            {
                "name": "For Each Table_Additional Companies",
                "type": "ForEach",
                "dependsOn": [
                    {
                        "activity": "For Each Table",
                        "dependencyConditions": [
                            "Succeeded"
                        ]
                    }
                ],
                "userProperties": [],
                "typeProperties": {
                    "items": {
                        "value": "@variables('VarQueryBase2')",
                        "type": "Expression"
                    },
                    "activities": [
                        {
                            "name": "GetLastTimestamp_Additional Companies",
                            "description": "Retrieve  the max rowversion value stored in the control table",
                            "type": "Lookup",
                            "dependsOn": [],
                            "policy": {
                                "timeout": "7.00:00:00",
                                "retry": 0,
                                "retryIntervalInSeconds": 30
                            },
                            "userProperties": [],
                            "typeProperties": {
                                "source": {
                                    "type": "AzureSqlSource",
                                    "sqlReaderQuery": {
                                        "value": "@concat('SELECT max([NewRowVersion])  AS LastTimestamp \n  FROM ', variables('Control_Table_Table_Name'), '\n  where [Table] =''',item().BaseTableName,''' AND [CompanyPrefix] = ''',item().TablePrefix, ''' ')",
                                        "type": "Expression"
                                    },
                                    "partitionOption": "None"
                                },
                                "dataset": {
                                    "referenceName": "LSInsightDW",
                                    "type": "DatasetReference",
                                    "parameters": {
                                        "DestTableName": "NA"
                                    }
                                },
                                "firstRowOnly": true
                            }
                        },
                        {
                            "name": "GetCurrentTimestamp_Additional Companies",
                            "description": "Retrieve  the current maximum value in watermark column of source data store",
                            "type": "Lookup",
                            "dependsOn": [],
                            "policy": {
                                "timeout": "7.00:00:00",
                                "retry": 0,
                                "retryIntervalInSeconds": 30
                            },
                            "userProperties": [],
                            "typeProperties": {
                                "source": {
                                    "type": "SqlServerSource",
                                    "sqlReaderQuery": {
                                        "value": "@concat('select COALESCE(MAX(',item().IncrementalKey, '),0) as CurrentTimestamp from ', \nitem().TableSnippet , ' ' ,item().JoinSnippet)",
                                        "type": "Expression"
                                    },
                                    "queryTimeout": "02:00:00",
                                    "partitionOption": "None"
                                },
                                "dataset": {
                                    "referenceName": "SourceDataset",
                                    "type": "DatasetReference",
                                    "parameters": {
                                        "dest_table": "NA"
                                    }
                                }
                            }
                        },
                        {
                            "name": "ErrorOnGetCurrentTimestamp_Additional Companies",
                            "description": "Stored procedure activity to store the new Pipeline run metadata in LSAudit table",
                            "type": "SqlServerStoredProcedure",
                            "dependsOn": [
                                {
                                    "activity": "GetCurrentTimestamp_Additional Companies",
                                    "dependencyConditions": [
                                        "Failed"
                                    ]
                                }
                            ],
                            "policy": {
                                "timeout": "7.00:00:00",
                                "retry": 0,
                                "retryIntervalInSeconds": 30
                            },
                            "userProperties": [],
                            "typeProperties": {
                                "storedProcedureName": "[[dbo].[UpdateLSInsightAudit]",
                                "storedProcedureParameters": {
                                    "ADFRunGUID": {
                                        "value": {
                                            "value": "@pipeline().RunId",
                                            "type": "Expression"
                                        },
                                        "type": "String"
                                    },
                                    "CopyDuration": {
                                        "value": {
                                            "value": "0",
                                            "type": "Expression"
                                        },
                                        "type": "Int32"
                                    },
                                    "PipelineName": {
                                        "value": {
                                            "value": "@concat('Failed Run - ',  pipeline().Pipeline)",
                                            "type": "Expression"
                                        },
                                        "type": "String"
                                    },
                                    "PipelineStart": {
                                        "value": {
                                            "value": "@variables('PipelineStart')",
                                            "type": "Expression"
                                        },
                                        "type": "DateTime"
                                    },
                                    "RowsCopied": {
                                        "value": {
                                            "value": "0",
                                            "type": "Expression"
                                        },
                                        "type": "Int32"
                                    },
                                    "RowsRead": {
                                        "value": {
                                            "value": "0",
                                            "type": "Expression"
                                        },
                                        "type": "Int32"
                                    },
                                    "RowVersion": {
                                        "value": {
                                            "value": "0",
                                            "type": "Expression"
                                        },
                                        "type": "Int64"
                                    },
                                    "Table": {
                                        "value": {
                                            "value": "@item().BaseTableName",
                                            "type": "Expression"
                                        },
                                        "type": "String"
                                    },
                                    "PipelineTriggerType": {
                                        "value": {
                                            "value": "@pipeline().TriggerType",
                                            "type": "Expression"
                                        },
                                        "type": "String"
                                    },
                                    "ErrorMessage": {
                                        "value": {
                                            "value": "@activity('GetCurrentTimestamp_Additional Companies').Error.Message",
                                            "type": "Expression"
                                        },
                                        "type": "string"
                                    },
                                    "CompanyPrefix": {
                                        "value": {
                                            "value": "@item().TablePrefix",
                                            "type": "Expression"
                                        },
                                        "type": "String"
                                    }
                                }
                            },
                            "linkedServiceName": {
                                "referenceName": "LSInsightDW",
                                "type": "LinkedServiceReference"
                            }
                        },
                        {
                            "name": "ErrorOnGetLastTimestamp_Additional Companies",
                            "description": "Stored procedure activity to store the new Pipeline run metadata in LSAudit table",
                            "type": "SqlServerStoredProcedure",
                            "dependsOn": [
                                {
                                    "activity": "GetLastTimestamp_Additional Companies",
                                    "dependencyConditions": [
                                        "Failed"
                                    ]
                                }
                            ],
                            "policy": {
                                "timeout": "7.00:00:00",
                                "retry": 0,
                                "retryIntervalInSeconds": 30
                            },
                            "userProperties": [],
                            "typeProperties": {
                                "storedProcedureName": "[[dbo].[UpdateLSInsightAudit]",
                                "storedProcedureParameters": {
                                    "ADFRunGUID": {
                                        "value": {
                                            "value": "@pipeline().RunId",
                                            "type": "Expression"
                                        },
                                        "type": "String"
                                    },
                                    "CopyDuration": {
                                        "value": {
                                            "value": "0",
                                            "type": "Expression"
                                        },
                                        "type": "Int32"
                                    },
                                    "PipelineName": {
                                        "value": {
                                            "value": "@concat('Failed Run - ',  pipeline().Pipeline)",
                                            "type": "Expression"
                                        },
                                        "type": "String"
                                    },
                                    "PipelineStart": {
                                        "value": {
                                            "value": "@variables('PipelineStart')",
                                            "type": "Expression"
                                        },
                                        "type": "DateTime"
                                    },
                                    "RowsCopied": {
                                        "value": {
                                            "value": "0",
                                            "type": "Expression"
                                        },
                                        "type": "Int32"
                                    },
                                    "RowsRead": {
                                        "value": {
                                            "value": "0",
                                            "type": "Expression"
                                        },
                                        "type": "Int32"
                                    },
                                    "RowVersion": {
                                        "value": {
                                            "value": "0",
                                            "type": "Expression"
                                        },
                                        "type": "Int64"
                                    },
                                    "Table": {
                                        "value": {
                                            "value": "@item().BaseTableName",
                                            "type": "Expression"
                                        },
                                        "type": "String"
                                    },
                                    "PipelineTriggerType": {
                                        "value": {
                                            "value": "@pipeline().TriggerType",
                                            "type": "Expression"
                                        },
                                        "type": "String"
                                    },
                                    "ErrorMessage": {
                                        "value": {
                                            "value": "@activity('GetLastTimestamp_Additional Companies').Error.Message",
                                            "type": "Expression"
                                        },
                                        "type": "string"
                                    },
                                    "CompanyPrefix": {
                                        "value": {
                                            "value": "@item().TablePrefix",
                                            "type": "Expression"
                                        },
                                        "type": "String"
                                    }
                                }
                            },
                            "linkedServiceName": {
                                "referenceName": "LSInsightDW",
                                "type": "LinkedServiceReference"
                            }
                        },
                        {
                            "name": "LoadDataToStagingTables_Additional Companies",
                            "type": "Copy",
                            "dependsOn": [
                                {
                                    "activity": "GetLastTimestamp_Additional Companies",
                                    "dependencyConditions": [
                                        "Succeeded"
                                    ]
                                },
                                {
                                    "activity": "GetCurrentTimestamp_Additional Companies",
                                    "dependencyConditions": [
                                        "Succeeded"
                                    ]
                                }
                            ],
                            "policy": {
                                "timeout": "7.00:00:00",
                                "retry": 0,
                                "retryIntervalInSeconds": 30,
                                "secureOutput": false,
                                "secureInput": false
                            },
                            "userProperties": [],
                            "typeProperties": {
                                "source": {
                                    "type": "SqlServerSource",
                                    "additionalColumns": [
                                        {
                                            "name": "CompanyPrefix",
                                            "value": {
                                                "value": "@item().TablePrefix",
                                                "type": "Expression"
                                            }
                                        }
                                    ],
                                    "sqlReaderQuery": {
                                        "value": "@concat(item().SelectStatement, \nif(\nlessOrEquals(item().SumCountRows,pipeline().parameters.FullLoadthreshold),' ',\nconcat(' AND ' , \nitem().IncrementalKey,' >  ',if(equals(activity('GetLastTimestamp_Additional Companies').output.firstRow.LastTimestamp,NULL),0,activity('GetLastTimestamp_Additional Companies').output.firstRow.LastTimestamp),' AND ', item().IncrementalKey  ,' <= ',if(equals(activity('GetCurrentTimestamp_Additional Companies').output.firstRow.CurrentTimestamp,NULL),0,activity('GetCurrentTimestamp_Additional Companies').output.firstRow.CurrentTimestamp))))",
                                        "type": "Expression"
                                    },
                                    "queryTimeout": "02:00:00",
                                    "partitionOption": "None"
                                },
                                "sink": {
                                    "type": "AzureSqlSink",
                                    "preCopyScript": {
                                        "value": "@{if(\nlessOrEquals(item().SumCountRows,pipeline().parameters.FullLoadthreshold)\n,'',\nconcat('DELETE FROM  [' , \nitem().DestinationTable, \n']  WHERE bigint_timestamp >  '\n,if(equals(activity('GetLastTimestamp_Additional Companies').output.firstRow.LastTimestamp,NULL),0,activity('GetLastTimestamp_Additional Companies').output.firstRow.LastTimestamp)\n,' AND bigint_timestamp <= ',if(equals(activity('GetCurrentTimestamp_Additional Companies').output.firstRow.CurrentTimestamp,NULL),0,activity('GetCurrentTimestamp_Additional Companies').output.firstRow.CurrentTimestamp)\n,' AND [CompanyPrefix] = ''',item().TablePrefix, ''''\n))\n}",
                                        "type": "Expression"
                                    },
                                    "disableMetricsCollection": false
                                },
                                "enableStaging": false,
                                "translator": {
                                    "type": "TabularTranslator",
                                    "typeConversion": true,
                                    "typeConversionSettings": {
                                        "allowDataTruncation": true,
                                        "treatBooleanAsNumber": false
                                    }
                                }
                            },
                            "inputs": [
                                {
                                    "referenceName": "SourceDataset",
                                    "type": "DatasetReference",
                                    "parameters": {
                                        "dest_table": {
                                            "value": "@item().DestinationTable",
                                            "type": "Expression"
                                        }
                                    }
                                }
                            ],
                            "outputs": [
                                {
                                    "referenceName": "LSInsightDW",
                                    "type": "DatasetReference",
                                    "parameters": {
                                        "DestTableName": {
                                            "value": "@item().DestinationTable",
                                            "type": "Expression"
                                        }
                                    }
                                }
                            ]
                        },
                        {
                            "name": "UpdateLSInsightAudit_Additional Companies",
                            "description": "Stored procedure activity to store the new Pipeline run metadata in LSAudit table",
                            "type": "SqlServerStoredProcedure",
                            "dependsOn": [
                                {
                                    "activity": "LoadDataToStagingTables_Additional Companies",
                                    "dependencyConditions": [
                                        "Succeeded"
                                    ]
                                }
                            ],
                            "policy": {
                                "timeout": "7.00:00:00",
                                "retry": 0,
                                "retryIntervalInSeconds": 30
                            },
                            "userProperties": [],
                            "typeProperties": {
                                "storedProcedureName": "[[dbo].[UpdateLSInsightAudit]",
                                "storedProcedureParameters": {
                                    "ADFRunGUID": {
                                        "value": {
                                            "value": "@pipeline().RunId",
                                            "type": "Expression"
                                        },
                                        "type": "String"
                                    },
                                    "CopyDuration": {
                                        "value": {
                                            "value": "@activity('LoadDataToStagingTables_Additional Companies').output.copyDuration",
                                            "type": "Expression"
                                        },
                                        "type": "Int32"
                                    },
                                    "PipelineName": {
                                        "value": {
                                            "value": "@pipeline().Pipeline",
                                            "type": "Expression"
                                        },
                                        "type": "String"
                                    },
                                    "PipelineStart": {
                                        "value": {
                                            "value": "@variables('PipelineStart')",
                                            "type": "Expression"
                                        },
                                        "type": "DateTime"
                                    },
                                    "RowsCopied": {
                                        "value": {
                                            "value": "@activity('LoadDataToStagingTables_Additional Companies').output.rowsCopied",
                                            "type": "Expression"
                                        },
                                        "type": "Int32"
                                    },
                                    "RowsRead": {
                                        "value": {
                                            "value": "@activity('LoadDataToStagingTables_Additional Companies').output.rowsRead",
                                            "type": "Expression"
                                        },
                                        "type": "Int32"
                                    },
                                    "RowVersion": {
                                        "value": {
                                            "value": "@activity('GetCurrentTimestamp_Additional Companies').output.firstRow.CurrentTimestamp",
                                            "type": "Expression"
                                        },
                                        "type": "Int64"
                                    },
                                    "Table": {
                                        "value": {
                                            "value": "@item().BaseTableName",
                                            "type": "Expression"
                                        },
                                        "type": "String"
                                    },
                                    "PipelineTriggerType": {
                                        "value": {
                                            "value": "@pipeline().TriggerType",
                                            "type": "Expression"
                                        },
                                        "type": "String"
                                    },
                                    "ErrorMessage": {
                                        "value": "",
                                        "type": "String"
                                    },
                                    "CompanyPrefix": {
                                        "value": {
                                            "value": "@item().TablePrefix",
                                            "type": "Expression"
                                        },
                                        "type": "String"
                                    },
                                    "Note": {
                                        "value": {
                                            "value": "@if(\nlessOrEquals(item().SumCountRows,pipeline().parameters.FullLoadthreshold),'Full load',concat('Incremental load. source row count estimate: ' , item().SumCountRows))",
                                            "type": "Expression"
                                        },
                                        "type": "String"
                                    }
                                }
                            },
                            "linkedServiceName": {
                                "referenceName": "LSInsightDW",
                                "type": "LinkedServiceReference"
                            }
                        },
                        {
                            "name": "OnCopyError_copy1",
                            "description": "Stored procedure activity to store the new Pipeline run metadata in LSAudit table On error",
                            "type": "SqlServerStoredProcedure",
                            "dependsOn": [
                                {
                                    "activity": "LoadDataToStagingTables_Additional Companies",
                                    "dependencyConditions": [
                                        "Failed"
                                    ]
                                }
                            ],
                            "policy": {
                                "timeout": "7.00:00:00",
                                "retry": 0,
                                "retryIntervalInSeconds": 30
                            },
                            "userProperties": [],
                            "typeProperties": {
                                "storedProcedureName": "[[dbo].[UpdateLSInsightAudit]",
                                "storedProcedureParameters": {
                                    "ADFRunGUID": {
                                        "value": {
                                            "value": "@pipeline().RunId",
                                            "type": "Expression"
                                        },
                                        "type": "String"
                                    },
                                    "CopyDuration": {
                                        "value": {
                                            "value": "0",
                                            "type": "Expression"
                                        },
                                        "type": "Int32"
                                    },
                                    "PipelineName": {
                                        "value": {
                                            "value": "@concat('Failed Run - ',  pipeline().Pipeline)",
                                            "type": "Expression"
                                        },
                                        "type": "String"
                                    },
                                    "PipelineStart": {
                                        "value": {
                                            "value": "@variables('PipelineStart')",
                                            "type": "Expression"
                                        },
                                        "type": "DateTime"
                                    },
                                    "RowsCopied": {
                                        "value": {
                                            "value": "0",
                                            "type": "Expression"
                                        },
                                        "type": "Int32"
                                    },
                                    "RowsRead": {
                                        "value": {
                                            "value": "0",
                                            "type": "Expression"
                                        },
                                        "type": "Int32"
                                    },
                                    "RowVersion": {
                                        "value": {
                                            "value": "0",
                                            "type": "Expression"
                                        },
                                        "type": "Int64"
                                    },
                                    "Table": {
                                        "value": {
                                            "value": "@item().BaseTableName",
                                            "type": "Expression"
                                        },
                                        "type": "String"
                                    },
                                    "PipelineTriggerType": {
                                        "value": {
                                            "value": "@pipeline().TriggerType",
                                            "type": "Expression"
                                        },
                                        "type": "String"
                                    },
                                    "ErrorMessage": {
                                        "value": {
                                            "value": "@activity('LoadDataToStagingTables_Additional Companies').Error.Message",
                                            "type": "Expression"
                                        },
                                        "type": "string"
                                    },
                                    "CompanyPrefix": {
                                        "value": {
                                            "value": "@item().TablePrefix",
                                            "type": "Expression"
                                        },
                                        "type": "String"
                                    }
                                }
                            },
                            "linkedServiceName": {
                                "referenceName": "LSInsightDW",
                                "type": "LinkedServiceReference"
                            }
                        }
                    ]
                }
            },
            {
                "name": "Append VarQueryBase",
                "type": "ForEach",
                "dependsOn": [
                    {
                        "activity": "Set varTables",
                        "dependencyConditions": [
                            "Succeeded"
                        ]
                    }
                ],
                "userProperties": [],
                "typeProperties": {
                    "items": {
                        "value": "@variables('VarQueryTables')",
                        "type": "Expression"
                    },
                    "isSequential": false,
                    "activities": [
                        {
                            "name": "GetLSInsightQueryBase",
                            "type": "Lookup",
                            "dependsOn": [],
                            "policy": {
                                "timeout": "7.00:00:00",
                                "retry": 0,
                                "retryIntervalInSeconds": 30,
                                "secureOutput": false,
                                "secureInput": false
                            },
                            "userProperties": [],
                            "typeProperties": {
                                "source": {
                                    "type": "AzureSqlSource",
                                    "sqlReaderQuery": {
                                        "value": "@concat('SELECT DISTINCT [BaseTableName]\n      ,[CountRows]\n,[SumCountRows]\n,[CountCompanies]\n,[CompanyNumber]\n     ,[TablePrefix]\n     ,[DestinationTable]\n     ,[ColumnSnippet]\n     ,[TableSnippet]\n     ,[JoinSnippet]\n     ,[SelectStatement]\n     ,[DropSnippet]\n     ,[CreateSnippet]\n     ,[DropAndCreate]\n     ,[IncrementalKey]\n  FROM [LSInsight$QUERY BASE]\nWHERE [BaseTableName] = ''',item().BaseTableName,'''\n  AND TablePrefix like ''', item().TablePrefix ,''' ')",
                                        "type": "Expression"
                                    },
                                    "queryTimeout": "02:00:00",
                                    "partitionOption": "None"
                                },
                                "dataset": {
                                    "referenceName": "LSInsightDW",
                                    "type": "DatasetReference",
                                    "parameters": {
                                        "DestTableName": "NA"
                                    }
                                },
                                "firstRowOnly": false
                            }
                        },
                        {
                            "name": "If Condition1",
                            "type": "IfCondition",
                            "dependsOn": [
                                {
                                    "activity": "GetLSInsightQueryBase",
                                    "dependencyConditions": [
                                        "Succeeded"
                                    ]
                                }
                            ],
                            "userProperties": [],
                            "typeProperties": {
                                "expression": {
                                    "value": "@greater(activity('GetLSInsightQueryBase').output.value[0].CompanyNumber,1)",
                                    "type": "Expression"
                                },
                                "ifFalseActivities": [
                                    {
                                        "name": "Append variable1_copy1",
                                        "type": "AppendVariable",
                                        "dependsOn": [],
                                        "userProperties": [],
                                        "typeProperties": {
                                            "variableName": "VarQueryBase1",
                                            "value": {
                                                "value": "@activity('GetLSInsightQueryBase').output.value[0]",
                                                "type": "Expression"
                                            }
                                        }
                                    }
                                ],
                                "ifTrueActivities": [
                                    {
                                        "name": "Append variable1",
                                        "type": "AppendVariable",
                                        "dependsOn": [],
                                        "userProperties": [],
                                        "typeProperties": {
                                            "variableName": "VarQueryBase2",
                                            "value": {
                                                "value": "@activity('GetLSInsightQueryBase').output.value[0]",
                                                "type": "Expression"
                                            }
                                        }
                                    }
                                ]
                            }
                        }
                    ]
                }
            },
            {
                "name": "Get Tables",
                "type": "Lookup",
                "dependsOn": [
                    {
                        "activity": "Update DestRowCount",
                        "dependencyConditions": [
                            "Succeeded"
                        ]
                    }
                ],
                "policy": {
                    "timeout": "7.00:00:00",
                    "retry": 0,
                    "retryIntervalInSeconds": 30,
                    "secureOutput": false,
                    "secureInput": false
                },
                "userProperties": [],
                "typeProperties": {
                    "source": {
                        "type": "AzureSqlSource",
                        "sqlReaderQuery": {
                            "value": "@concat(\n'SELECT distinct [BaseTableName] \n,[TablePrefix]\n  FROM [dbo].[LSInsight$QUERY BASE]\n  WHERE [BaseTableName] like ''',pipeline().parameters.TableToLoad,'''\nAND TablePrefix like ''',pipeline().parameters.CompanyToLoad,''' ')",
                            "type": "Expression"
                        },
                        "queryTimeout": "02:00:00",
                        "partitionOption": "None"
                    },
                    "dataset": {
                        "referenceName": "LSInsightDW",
                        "type": "DatasetReference",
                        "parameters": {
                            "DestTableName": "Na"
                        }
                    },
                    "firstRowOnly": false
                }
            },
            {
                "name": "Set varTables",
                "type": "SetVariable",
                "dependsOn": [
                    {
                        "activity": "Get Tables",
                        "dependencyConditions": [
                            "Succeeded"
                        ]
                    }
                ],
                "userProperties": [],
                "typeProperties": {
                    "variableName": "VarQueryTables",
                    "value": {
                        "value": "@activity('Get Tables').output.value",
                        "type": "Expression"
                    }
                }
            },
            {
                "name": "Update DestRowCount",
                "type": "Lookup",
                "dependsOn": [
                    {
                        "activity": "PipelineStart",
                        "dependencyConditions": [
                            "Succeeded"
                        ]
                    }
                ],
                "policy": {
                    "timeout": "7.00:00:00",
                    "retry": 0,
                    "retryIntervalInSeconds": 30,
                    "secureOutput": false,
                    "secureInput": false
                },
                "userProperties": [],
                "typeProperties": {
                    "source": {
                        "type": "AzureSqlSource",
                        "sqlReaderQuery": "WITH tDestRowCount\nAS\n(SELECT DISTINCT\n\t\tp.rows\n\t   ,t.name\n\tFROM sys.partitions p\n\t\t,sys.tables t\n\tWHERE p.object_id = t.object_id\n\tAND t.name LIKE 'stg$%')\nMERGE [LSInsight$QUERY BASE] AS Target USING tDestRowCount AS Source\nON Source.name = Target.DestinationTable\nWHEN MATCHED\n\tTHEN UPDATE\n\t\tSET Target.SumCountRows = Source.rows\nWHEN NOT MATCHED BY Source\nTHEN UPDATE\n\t\tSET Target.SumCountRows = 0\n;\n\nSELECT\n\t1 AS [Return Value]",
                        "queryTimeout": "02:00:00",
                        "partitionOption": "None"
                    },
                    "dataset": {
                        "referenceName": "LSInsightDW",
                        "type": "DatasetReference",
                        "parameters": {
                            "DestTableName": "NA"
                        }
                    }
                }
            }
        ],
        "parameters": {
            "TableToLoad": {
                "type": "string",
                "defaultValue": "%"
            },
            "CompanyToLoad": {
                "type": "string",
                "defaultValue": "%"
            },
            "FullLoadthreshold": {
                "type": "int",
                "defaultValue": 100000
            }
        },
        "variables": {
            "PipelineStart": {
                "type": "String"
            },
            "ErrorMessage": {
                "type": "String"
            },
            "Control_Table_Table_Name": {
                "type": "String",
                "defaultValue": "[[dbo].[LSInsightAudit]"
            },
            "VarQueryBase": {
                "type": "Array"
            },
            "VarQueryBase2": {
                "type": "Array"
            },
            "VarQueryBase1": {
                "type": "Array"
            },
            "VarQueryTables": {
                "type": "Array"
            }
        },
        "folder": {
            "name": "2 - Staging Tables"
        },
        "annotations": [],
        "lastPublishTime": "2020-10-30T09:54:30Z"
    },
            "dependsOn": [
                "[concat(variables('factoryId'), '/datasets/LSInsightDW')]",
                "[concat(variables('factoryId'), '/datasets/SourceDataset')]",
                "[concat(variables('factoryId'), '/datasets/LSCentralColumnMetadata')]",
                "[concat(variables('factoryId'), '/linkedServices/LSInsightDW')]"
            ]
        },
        {
        "name": "[concat(parameters('factoryName'), '/PL-SP-factHotelStatistic')]",
        "type": "Microsoft.DataFactory/factories/pipelines",
        "apiVersion": "2018-06-01",
        "properties": {
        "description": "Execute Stored Procedure in LS Insight DW",
        "activities": [
            {
                "name": "Last-RowID",
                "description": "Retrieve  the max rowversion value stored in the control table",
                "type": "Lookup",
                "dependsOn": [
                    {
                        "activity": "PipelineStart",
                        "dependencyConditions": [
                            "Succeeded"
                        ]
                    }
                ],
                "policy": {
                    "timeout": "7.00:00:00",
                    "retry": 0,
                    "retryIntervalInSeconds": 30
                },
                "userProperties": [],
                "typeProperties": {
                    "source": {
                        "type": "AzureSqlSource",
                        "sqlReaderQuery": {
                            "value": "@concat('SELECT COALESCE(max([NewRowVersion]),0)  AS LastRowID \n  FROM ', pipeline().parameters.Control_Table_Table_Name, '\n  where [Table] =''',variables('DestinationTable'),'''')",
                            "type": "Expression"
                        },
                        "partitionOption": "None"
                    },
                    "dataset": {
                        "referenceName": "LSInsightDW",
                        "type": "DatasetReference",
                        "parameters": {
                            "DestTableName": "NA"
                        }
                    },
                    "firstRowOnly": true
                }
            },
            {
                "name": "UpdateLSInsightAudit-TSE",
                "description": "Stored procedure activity to store the new Pipeline run metadata in LSAudit table",
                "type": "SqlServerStoredProcedure",
                "dependsOn": [
                    {
                        "activity": "Execute Stored Procedure",
                        "dependencyConditions": [
                            "Succeeded"
                        ]
                    }
                ],
                "policy": {
                    "timeout": "7.00:00:00",
                    "retry": 0,
                    "retryIntervalInSeconds": 30
                },
                "userProperties": [],
                "typeProperties": {
                    "storedProcedureName": "[[dbo].[UpdateLSInsightAudit]",
                    "storedProcedureParameters": {
                        "ADFRunGUID": {
                            "value": {
                                "value": "@pipeline().RunId",
                                "type": "Expression"
                            },
                            "type": "String"
                        },
                        "CopyDuration": {
                            "value": null,
                            "type": "Int32"
                        },
                        "PipelineName": {
                            "value": {
                                "value": "@pipeline().Pipeline",
                                "type": "Expression"
                            },
                            "type": "String"
                        },
                        "PipelineStart": {
                            "value": {
                                "value": "@variables('PipelineStart')",
                                "type": "Expression"
                            },
                            "type": "DateTime"
                        },
                        "RowsCopied": {
                            "value": {
                                "value": "@activity('Execute Stored Procedure').output.firstRow.RowCount",
                                "type": "Expression"
                            },
                            "type": "Int32"
                        },
                        "RowsRead": {
                            "value": null,
                            "type": "Int32"
                        },
                        "RowVersion": {
                            "value": {
                                "value": "@activity('Current-RowID').output.firstRow.NewRowID",
                                "type": "Expression"
                            },
                            "type": "Int64"
                        },
                        "Table": {
                            "value": {
                                "value": "@variables('DestinationTable')",
                                "type": "Expression"
                            },
                            "type": "String"
                        },
                        "PipelineTriggerType": {
                            "value": {
                                "value": "@pipeline().TriggerType",
                                "type": "Expression"
                            },
                            "type": "String"
                        },
                        "ErrorMessage": {
                            "value": null,
                            "type": "String"
                        },
                        "Note": {
                            "value": null
                        }
                    }
                },
                "linkedServiceName": {
                    "referenceName": "LSInsightDW",
                    "type": "LinkedServiceReference"
                }
            },
            {
                "name": "PipelineStart",
                "type": "SetVariable",
                "dependsOn": [],
                "userProperties": [],
                "typeProperties": {
                    "variableName": "PipelineStart",
                    "value": {
                        "value": "@utcnow()",
                        "type": "Expression"
                    }
                }
            },
            {
                "name": "SqlServerStoredProcedure1",
                "description": "Stored procedure activity to store the new Pipeline run metadata in LSAudit table",
                "type": "SqlServerStoredProcedure",
                "dependsOn": [
                    {
                        "activity": "Execute Stored Procedure",
                        "dependencyConditions": [
                            "Failed"
                        ]
                    }
                ],
                "policy": {
                    "timeout": "7.00:00:00",
                    "retry": 0,
                    "retryIntervalInSeconds": 30
                },
                "userProperties": [],
                "typeProperties": {
                    "storedProcedureName": "[[dbo].[UpdateLSInsightAudit]",
                    "storedProcedureParameters": {
                        "ADFRunGUID": {
                            "value": {
                                "value": "@pipeline().RunId",
                                "type": "Expression"
                            },
                            "type": "String"
                        },
                        "CopyDuration": {
                            "value": {
                                "value": "0",
                                "type": "Expression"
                            },
                            "type": "Int32"
                        },
                        "PipelineName": {
                            "value": {
                                "value": "@concat('Failed Run - ',  pipeline().Pipeline)",
                                "type": "Expression"
                            },
                            "type": "String"
                        },
                        "PipelineStart": {
                            "value": {
                                "value": "@variables('PipelineStart')",
                                "type": "Expression"
                            },
                            "type": "DateTime"
                        },
                        "RowsCopied": {
                            "value": {
                                "value": "0",
                                "type": "Expression"
                            },
                            "type": "Int32"
                        },
                        "RowsRead": {
                            "value": {
                                "value": "0",
                                "type": "Expression"
                            },
                            "type": "Int32"
                        },
                        "RowVersion": {
                            "value": {
                                "value": "0",
                                "type": "Expression"
                            },
                            "type": "Int64"
                        },
                        "Table": {
                            "value": {
                                "value": "@variables('Data_Source_Table_Name')",
                                "type": "Expression"
                            },
                            "type": "String"
                        },
                        "PipelineTriggerType": {
                            "value": {
                                "value": "@pipeline().TriggerType",
                                "type": "Expression"
                            },
                            "type": "String"
                        },
                        "ErrorMessage": {
                            "value": {
                                "value": "@activity('Execute Stored Procedure').error.message",
                                "type": "Expression"
                            },
                            "type": "string"
                        }
                    }
                },
                "linkedServiceName": {
                    "referenceName": "LSInsightDW",
                    "type": "LinkedServiceReference"
                }
            },
            {
                "name": "Last-TSE-RowID-Error",
                "description": "Stored procedure activity to store the new Pipeline run metadata in LSAudit table",
                "type": "SqlServerStoredProcedure",
                "dependsOn": [
                    {
                        "activity": "Last-RowID",
                        "dependencyConditions": [
                            "Failed"
                        ]
                    }
                ],
                "policy": {
                    "timeout": "7.00:00:00",
                    "retry": 0,
                    "retryIntervalInSeconds": 30
                },
                "userProperties": [],
                "typeProperties": {
                    "storedProcedureName": "[[dbo].[UpdateLSInsightAudit]",
                    "storedProcedureParameters": {
                        "ADFRunGUID": {
                            "value": {
                                "value": "@pipeline().RunId",
                                "type": "Expression"
                            },
                            "type": "String"
                        },
                        "CopyDuration": {
                            "value": {
                                "value": "0",
                                "type": "Expression"
                            },
                            "type": "Int32"
                        },
                        "PipelineName": {
                            "value": {
                                "value": "@concat('Failed Run - ',  pipeline().Pipeline)",
                                "type": "Expression"
                            },
                            "type": "String"
                        },
                        "PipelineStart": {
                            "value": {
                                "value": "@variables('PipelineStart')",
                                "type": "Expression"
                            },
                            "type": "DateTime"
                        },
                        "RowsCopied": {
                            "value": {
                                "value": "0",
                                "type": "Expression"
                            },
                            "type": "Int32"
                        },
                        "RowsRead": {
                            "value": {
                                "value": "0",
                                "type": "Expression"
                            },
                            "type": "Int32"
                        },
                        "RowVersion": {
                            "value": {
                                "value": "0",
                                "type": "Expression"
                            },
                            "type": "Int64"
                        },
                        "Table": {
                            "value": {
                                "value": "@variables('DestinationTable')",
                                "type": "Expression"
                            },
                            "type": "String"
                        },
                        "PipelineTriggerType": {
                            "value": {
                                "value": "@pipeline().TriggerType",
                                "type": "Expression"
                            },
                            "type": "String"
                        },
                        "ErrorMessage": {
                            "value": {
                                "value": "@activity('Last-RowID').output.Error.Message",
                                "type": "Expression"
                            },
                            "type": "string"
                        },
                        "Note": {
                            "value": "TSE"
                        }
                    }
                },
                "linkedServiceName": {
                    "referenceName": "LSInsightDW",
                    "type": "LinkedServiceReference"
                }
            },
            {
                "name": "Execute Stored Procedure",
                "type": "Lookup",
                "dependsOn": [
                    {
                        "activity": "Last-RowID",
                        "dependencyConditions": [
                            "Succeeded"
                        ]
                    },
                    {
                        "activity": "Current-RowID",
                        "dependencyConditions": [
                            "Succeeded"
                        ]
                    }
                ],
                "policy": {
                    "timeout": "7.00:00:00",
                    "retry": 0,
                    "retryIntervalInSeconds": 30,
                    "secureOutput": false,
                    "secureInput": false
                },
                "userProperties": [],
                "typeProperties": {
                    "source": {
                        "type": "AzureSqlSource",
                        "sqlReaderStoredProcedureName": "[[dbo].[factHotelStatistic]",
                        "queryTimeout": "23:00:00",
                        "partitionOption": "None"
                    },
                    "dataset": {
                        "referenceName": "LSInsightDW",
                        "type": "DatasetReference",
                        "parameters": {
                            "DestTableName": "NA"
                        }
                    }
                }
            },
            {
                "name": "Current-RowID",
                "description": "Retrieve  the current maximum value in watermark column of source data store",
                "type": "Lookup",
                "dependsOn": [
                    {
                        "activity": "PipelineStart",
                        "dependencyConditions": [
                            "Succeeded"
                        ]
                    }
                ],
                "policy": {
                    "timeout": "7.00:00:00",
                    "retry": 0,
                    "retryIntervalInSeconds": 30
                },
                "userProperties": [],
                "typeProperties": {
                    "source": {
                        "type": "AzureSqlSource",
                        "sqlReaderQuery": {
                            "value": "@concat('IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(''',variables('Data_Source_Table_Name'),'''))  select MAX(convert(bigint, ',pipeline().parameters.Data_Source_WaterMarkColumn, ')) as NewRowID from ',variables('Data_Source_Table_Name'), ' ELSE Select 0 AS NewRowID ')",
                            "type": "Expression"
                        },
                        "partitionOption": "None"
                    },
                    "dataset": {
                        "referenceName": "LSInsightDW",
                        "type": "DatasetReference",
                        "parameters": {
                            "DestTableName": "NA"
                        }
                    }
                }
            },
            {
                "name": "Current-RowID-TSE-Error",
                "description": "Stored procedure activity to store the new Pipeline run metadata in LSAudit table",
                "type": "SqlServerStoredProcedure",
                "dependsOn": [
                    {
                        "activity": "Current-RowID",
                        "dependencyConditions": [
                            "Failed"
                        ]
                    }
                ],
                "policy": {
                    "timeout": "7.00:00:00",
                    "retry": 0,
                    "retryIntervalInSeconds": 30
                },
                "userProperties": [],
                "typeProperties": {
                    "storedProcedureName": "[[dbo].[UpdateLSInsightAudit]",
                    "storedProcedureParameters": {
                        "ADFRunGUID": {
                            "value": {
                                "value": "@pipeline().RunId",
                                "type": "Expression"
                            },
                            "type": "String"
                        },
                        "CopyDuration": {
                            "value": {
                                "value": "0",
                                "type": "Expression"
                            },
                            "type": "Int32"
                        },
                        "PipelineName": {
                            "value": {
                                "value": "@concat('Failed Run - ',  pipeline().Pipeline)",
                                "type": "Expression"
                            },
                            "type": "String"
                        },
                        "PipelineStart": {
                            "value": {
                                "value": "@variables('PipelineStart')",
                                "type": "Expression"
                            },
                            "type": "DateTime"
                        },
                        "RowsCopied": {
                            "value": {
                                "value": "0",
                                "type": "Expression"
                            },
                            "type": "Int32"
                        },
                        "RowsRead": {
                            "value": {
                                "value": "0",
                                "type": "Expression"
                            },
                            "type": "Int32"
                        },
                        "RowVersion": {
                            "value": {
                                "value": "0",
                                "type": "Expression"
                            },
                            "type": "Int64"
                        },
                        "Table": {
                            "value": {
                                "value": "@variables('DestinationTable')",
                                "type": "Expression"
                            },
                            "type": "String"
                        },
                        "PipelineTriggerType": {
                            "value": {
                                "value": "@pipeline().TriggerType",
                                "type": "Expression"
                            },
                            "type": "String"
                        },
                        "ErrorMessage": {
                            "value": {
                                "value": "@activity('Current-RowID').output.Error.Message",
                                "type": "Expression"
                            },
                            "type": "string"
                        },
                        "Note": {
                            "value": "TSE"
                        }
                    }
                },
                "linkedServiceName": {
                    "referenceName": "LSInsightDW",
                    "type": "LinkedServiceReference"
                }
            }
        ],
        "parameters": {
            "Data_Source_WaterMarkColumn": {
                "type": "String",
                "defaultValue": "[[bigint_timestamp]"
            },
            "Control_Table_Table_Name": {
                "type": "String",
                "defaultValue": "[[dbo].[LSInsightAudit]"
            },
            "Control_Table_Column_Name": {
                "type": "String",
                "defaultValue": "[[NewRowVersion]"
            },
            "TablePrefix": {
                "type": "string"
            }
        },
        "variables": {
            "PipelineStart": {
                "type": "String"
            },
            "DestinationTable": {
                "type": "String",
                "defaultValue": "[[DW].[fHotelStatistic]"
            },
            "Data_Source_Table_Name": {
                "type": "String",
                "defaultValue": "[[Stg$Hotel Res_ Statistic]"
            },
            "ErrorMessage": {
                "type": "String"
            }
        },
        "folder": {
            "name": "4.e - Extension Fact Tables"
        },
        "annotations": [],
        "lastPublishTime": "2020-12-17T11:51:30Z"
        },
        "dependsOn": [
                "[concat(variables('factoryId'), '/datasets/LSInsightDW')]",
                "[concat(variables('factoryId'), '/linkedServices/LSInsightDW')]"
            ]
        },
        {
    "name": "[concat(parameters('factoryName'), '/PL-SP-factHotelDetailedRevenue')]",
	"type": "Microsoft.DataFactory/factories/pipelines",
    "apiVersion": "2018-06-01",
    "properties": {
        "description": "Execute Stored Procedure in LS Insight DW",
        "activities": [
            {
                "name": "Last-RowID",
                "description": "Retrieve  the max rowversion value stored in the control table",
                "type": "Lookup",
                "dependsOn": [
                    {
                        "activity": "PipelineStart",
                        "dependencyConditions": [
                            "Succeeded"
                        ]
                    }
                ],
                "policy": {
                    "timeout": "7.00:00:00",
                    "retry": 0,
                    "retryIntervalInSeconds": 30
                },
                "userProperties": [],
                "typeProperties": {
                    "source": {
                        "type": "AzureSqlSource",
                        "sqlReaderQuery": {
                            "value": "@concat('SELECT COALESCE(max([NewRowVersion]),0)  AS LastRowID \n  FROM ', pipeline().parameters.Control_Table_Table_Name, '\n  where [Table] =''',variables('DestinationTable'),'''')",
                            "type": "Expression"
                        },
                        "partitionOption": "None"
                    },
                    "dataset": {
                        "referenceName": "LSInsightDW",
                        "type": "DatasetReference",
                        "parameters": {
                            "DestTableName": "NA"
                        }
                    },
                    "firstRowOnly": true
                }
            },
            {
                "name": "UpdateLSInsightAudit-TSE",
                "description": "Stored procedure activity to store the new Pipeline run metadata in LSAudit table",
                "type": "SqlServerStoredProcedure",
                "dependsOn": [
                    {
                        "activity": "Execute Stored Procedure",
                        "dependencyConditions": [
                            "Succeeded"
                        ]
                    }
                ],
                "policy": {
                    "timeout": "7.00:00:00",
                    "retry": 0,
                    "retryIntervalInSeconds": 30
                },
                "userProperties": [],
                "typeProperties": {
                    "storedProcedureName": "[[dbo].[UpdateLSInsightAudit]",
                    "storedProcedureParameters": {
                        "ADFRunGUID": {
                            "value": {
                                "value": "@pipeline().RunId",
                                "type": "Expression"
                            },
                            "type": "String"
                        },
                        "CopyDuration": {
                            "value": null,
                            "type": "Int32"
                        },
                        "PipelineName": {
                            "value": {
                                "value": "@pipeline().Pipeline",
                                "type": "Expression"
                            },
                            "type": "String"
                        },
                        "PipelineStart": {
                            "value": {
                                "value": "@variables('PipelineStart')",
                                "type": "Expression"
                            },
                            "type": "DateTime"
                        },
                        "RowsCopied": {
                            "value": {
                                "value": "@activity('Execute Stored Procedure').output.firstRow.RowCount",
                                "type": "Expression"
                            },
                            "type": "Int32"
                        },
                        "RowsRead": {
                            "value": null,
                            "type": "Int32"
                        },
                        "RowVersion": {
                            "value": {
                                "value": "@activity('Current-RowID').output.firstRow.NewRowID",
                                "type": "Expression"
                            },
                            "type": "Int64"
                        },
                        "Table": {
                            "value": {
                                "value": "@variables('DestinationTable')",
                                "type": "Expression"
                            },
                            "type": "String"
                        },
                        "PipelineTriggerType": {
                            "value": {
                                "value": "@pipeline().TriggerType",
                                "type": "Expression"
                            },
                            "type": "String"
                        },
                        "ErrorMessage": {
                            "value": null,
                            "type": "String"
                        },
                        "Note": {
                            "value": null
                        }
                    }
                },
                "linkedServiceName": {
                    "referenceName": "LSInsightDW",
                    "type": "LinkedServiceReference"
                }
            },
            {
                "name": "PipelineStart",
                "type": "SetVariable",
                "dependsOn": [],
                "userProperties": [],
                "typeProperties": {
                    "variableName": "PipelineStart",
                    "value": {
                        "value": "@utcnow()",
                        "type": "Expression"
                    }
                }
            },
            {
                "name": "SqlServerStoredProcedure1",
                "description": "Stored procedure activity to store the new Pipeline run metadata in LSAudit table",
                "type": "SqlServerStoredProcedure",
                "dependsOn": [
                    {
                        "activity": "Execute Stored Procedure",
                        "dependencyConditions": [
                            "Failed"
                        ]
                    }
                ],
                "policy": {
                    "timeout": "7.00:00:00",
                    "retry": 0,
                    "retryIntervalInSeconds": 30
                },
                "userProperties": [],
                "typeProperties": {
                    "storedProcedureName": "[[dbo].[UpdateLSInsightAudit]",
                    "storedProcedureParameters": {
                        "ADFRunGUID": {
                            "value": {
                                "value": "@pipeline().RunId",
                                "type": "Expression"
                            },
                            "type": "String"
                        },
                        "CopyDuration": {
                            "value": {
                                "value": "0",
                                "type": "Expression"
                            },
                            "type": "Int32"
                        },
                        "PipelineName": {
                            "value": {
                                "value": "@concat('Failed Run - ',  pipeline().Pipeline)",
                                "type": "Expression"
                            },
                            "type": "String"
                        },
                        "PipelineStart": {
                            "value": {
                                "value": "@variables('PipelineStart')",
                                "type": "Expression"
                            },
                            "type": "DateTime"
                        },
                        "RowsCopied": {
                            "value": {
                                "value": "0",
                                "type": "Expression"
                            },
                            "type": "Int32"
                        },
                        "RowsRead": {
                            "value": {
                                "value": "0",
                                "type": "Expression"
                            },
                            "type": "Int32"
                        },
                        "RowVersion": {
                            "value": {
                                "value": "0",
                                "type": "Expression"
                            },
                            "type": "Int64"
                        },
                        "Table": {
                            "value": {
                                "value": "@variables('Data_Source_Table_Name')",
                                "type": "Expression"
                            },
                            "type": "String"
                        },
                        "PipelineTriggerType": {
                            "value": {
                                "value": "@pipeline().TriggerType",
                                "type": "Expression"
                            },
                            "type": "String"
                        },
                        "ErrorMessage": {
                            "value": {
                                "value": "@activity('Execute Stored Procedure').error.message",
                                "type": "Expression"
                            },
                            "type": "string"
                        }
                    }
                },
                "linkedServiceName": {
                    "referenceName": "LSInsightDW",
                    "type": "LinkedServiceReference"
                }
            },
            {
                "name": "Last-TSE-RowID-Error",
                "description": "Stored procedure activity to store the new Pipeline run metadata in LSAudit table",
                "type": "SqlServerStoredProcedure",
                "dependsOn": [
                    {
                        "activity": "Last-RowID",
                        "dependencyConditions": [
                            "Failed"
                        ]
                    }
                ],
                "policy": {
                    "timeout": "7.00:00:00",
                    "retry": 0,
                    "retryIntervalInSeconds": 30
                },
                "userProperties": [],
                "typeProperties": {
                    "storedProcedureName": "[[dbo].[UpdateLSInsightAudit]",
                    "storedProcedureParameters": {
                        "ADFRunGUID": {
                            "value": {
                                "value": "@pipeline().RunId",
                                "type": "Expression"
                            },
                            "type": "String"
                        },
                        "CopyDuration": {
                            "value": {
                                "value": "0",
                                "type": "Expression"
                            },
                            "type": "Int32"
                        },
                        "PipelineName": {
                            "value": {
                                "value": "@concat('Failed Run - ',  pipeline().Pipeline)",
                                "type": "Expression"
                            },
                            "type": "String"
                        },
                        "PipelineStart": {
                            "value": {
                                "value": "@variables('PipelineStart')",
                                "type": "Expression"
                            },
                            "type": "DateTime"
                        },
                        "RowsCopied": {
                            "value": {
                                "value": "0",
                                "type": "Expression"
                            },
                            "type": "Int32"
                        },
                        "RowsRead": {
                            "value": {
                                "value": "0",
                                "type": "Expression"
                            },
                            "type": "Int32"
                        },
                        "RowVersion": {
                            "value": {
                                "value": "0",
                                "type": "Expression"
                            },
                            "type": "Int64"
                        },
                        "Table": {
                            "value": {
                                "value": "@variables('DestinationTable')",
                                "type": "Expression"
                            },
                            "type": "String"
                        },
                        "PipelineTriggerType": {
                            "value": {
                                "value": "@pipeline().TriggerType",
                                "type": "Expression"
                            },
                            "type": "String"
                        },
                        "ErrorMessage": {
                            "value": {
                                "value": "@activity('Last-RowID').output.Error.Message",
                                "type": "Expression"
                            },
                            "type": "string"
                        },
                        "Note": {
                            "value": "TSE"
                        }
                    }
                },
                "linkedServiceName": {
                    "referenceName": "LSInsightDW",
                    "type": "LinkedServiceReference"
                }
            },
            {
                "name": "Execute Stored Procedure",
                "type": "Lookup",
                "dependsOn": [
                    {
                        "activity": "Last-RowID",
                        "dependencyConditions": [
                            "Succeeded"
                        ]
                    },
                    {
                        "activity": "Current-RowID",
                        "dependencyConditions": [
                            "Succeeded"
                        ]
                    }
                ],
                "policy": {
                    "timeout": "7.00:00:00",
                    "retry": 0,
                    "retryIntervalInSeconds": 30,
                    "secureOutput": false,
                    "secureInput": false
                },
                "userProperties": [],
                "typeProperties": {
                    "source": {
                        "type": "AzureSqlSource",
                        "sqlReaderStoredProcedureName": "[[dbo].[factHotelDetailedRevenue]",
                        "storedProcedureParameters": {
                            "HDRNewEntryNo": {
                                "type": "Int64",
                                "value": {
                                    "value": "@activity('Current-RowID').output.firstRow.NewRowID",
                                    "type": "Expression"
                                }
                            },
                            "HDREntryNo": {
                                "type": "Int64",
                                "value": {
                                    "value": "@activity('Last-RowID').output.firstRow.LastRowID",
                                    "type": "Expression"
                                }
                            }
                        },
                        "queryTimeout": "23:00:00",
                        "partitionOption": "None"
                    },
                    "dataset": {
                        "referenceName": "LSInsightDW",
                        "type": "DatasetReference",
                        "parameters": {
                            "DestTableName": "NA"
                        }
                    }
                }
            },
            {
                "name": "Current-RowID",
                "description": "Retrieve  the current maximum value in watermark column of source data store",
                "type": "Lookup",
                "dependsOn": [
                    {
                        "activity": "PipelineStart",
                        "dependencyConditions": [
                            "Succeeded"
                        ]
                    }
                ],
                "policy": {
                    "timeout": "7.00:00:00",
                    "retry": 0,
                    "retryIntervalInSeconds": 30
                },
                "userProperties": [],
                "typeProperties": {
                    "source": {
                        "type": "AzureSqlSource",
                        "sqlReaderQuery": {
                            "value": "@concat('IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(''',variables('Data_Source_Table_Name'),'''))  select MAX(convert(bigint, ',pipeline().parameters.Data_Source_WaterMarkColumn, ')) as NewRowID from ',variables('Data_Source_Table_Name'), ' ELSE Select 0 AS NewRowID ')",
                            "type": "Expression"
                        },
                        "partitionOption": "None"
                    },
                    "dataset": {
                        "referenceName": "LSInsightDW",
                        "type": "DatasetReference",
                        "parameters": {
                            "DestTableName": "NA"
                        }
                    }
                }
            },
            {
                "name": "Current-RowID-TSE-Error",
                "description": "Stored procedure activity to store the new Pipeline run metadata in LSAudit table",
                "type": "SqlServerStoredProcedure",
                "dependsOn": [
                    {
                        "activity": "Current-RowID",
                        "dependencyConditions": [
                            "Failed"
                        ]
                    }
                ],
                "policy": {
                    "timeout": "7.00:00:00",
                    "retry": 0,
                    "retryIntervalInSeconds": 30
                },
                "userProperties": [],
                "typeProperties": {
                    "storedProcedureName": "[[dbo].[UpdateLSInsightAudit]",
                    "storedProcedureParameters": {
                        "ADFRunGUID": {
                            "value": {
                                "value": "@pipeline().RunId",
                                "type": "Expression"
                            },
                            "type": "String"
                        },
                        "CopyDuration": {
                            "value": {
                                "value": "0",
                                "type": "Expression"
                            },
                            "type": "Int32"
                        },
                        "PipelineName": {
                            "value": {
                                "value": "@concat('Failed Run - ',  pipeline().Pipeline)",
                                "type": "Expression"
                            },
                            "type": "String"
                        },
                        "PipelineStart": {
                            "value": {
                                "value": "@variables('PipelineStart')",
                                "type": "Expression"
                            },
                            "type": "DateTime"
                        },
                        "RowsCopied": {
                            "value": {
                                "value": "0",
                                "type": "Expression"
                            },
                            "type": "Int32"
                        },
                        "RowsRead": {
                            "value": {
                                "value": "0",
                                "type": "Expression"
                            },
                            "type": "Int32"
                        },
                        "RowVersion": {
                            "value": {
                                "value": "0",
                                "type": "Expression"
                            },
                            "type": "Int64"
                        },
                        "Table": {
                            "value": {
                                "value": "@variables('DestinationTable')",
                                "type": "Expression"
                            },
                            "type": "String"
                        },
                        "PipelineTriggerType": {
                            "value": {
                                "value": "@pipeline().TriggerType",
                                "type": "Expression"
                            },
                            "type": "String"
                        },
                        "ErrorMessage": {
                            "value": {
                                "value": "@activity('Current-RowID').output.Error.Message",
                                "type": "Expression"
                            },
                            "type": "string"
                        },
                        "Note": {
                            "value": "TSE"
                        }
                    }
                },
                "linkedServiceName": {
                    "referenceName": "LSInsightDW",
                    "type": "LinkedServiceReference"
                }
            }
        ],
        "parameters": {
            "Data_Source_WaterMarkColumn": {
                "type": "String",
                "defaultValue": "[[bigint_timestamp]"
            },
            "Control_Table_Table_Name": {
                "type": "String",
                "defaultValue": "[[dbo].[LSInsightAudit]"
            },
            "Control_Table_Column_Name": {
                "type": "String",
                "defaultValue": "[[NewRowVersion]"
            },
            "TablePrefix": {
                "type": "string"
            }
        },
        "variables": {
            "PipelineStart": {
                "type": "String"
            },
            "DestinationTable": {
                "type": "String",
                "defaultValue": "[[DW].[fHotelDetailedRevenue]"
            },
            "Data_Source_Table_Name": {
                "type": "String",
                "defaultValue": "[[Stg$Detailed Revenue Entry]"
            },
            "ErrorMessage": {
                "type": "String"
            }
        },
        "folder": {
            "name": "4.e - Extension Fact Tables"
        },
        "annotations": [],
        "lastPublishTime": "2020-12-17T11:51:30Z"
        },
            "dependsOn": [
                "[concat(variables('factoryId'), '/datasets/LSInsightDW')]",
                "[concat(variables('factoryId'), '/linkedServices/LSInsightDW')]"
            ]
        },
		{
            "name": "[concat(parameters('factoryName'), '/Add or Delete Companies')]",
            "type": "Microsoft.DataFactory/factories/pipelines",
            "apiVersion": "2018-06-01",
            "properties": {
        "description": "Pipeline to set the Companies to use in LS Insight staging tables",
        "activities": [
            {
                "name": "Set Companies Array",
                "type": "SetVariable",
                "dependsOn": [],
                "userProperties": [],
                "typeProperties": {
                    "variableName": "CompaniesArray",
                    "value": {
                        "value": "@split(replace(replace(replace(pipeline().parameters.Companies,' ,',','),' , ',','),', ',','),',')",
                        "type": "Expression"
                    }
                }
            },
            {
                "name": "ForEach Company",
                "type": "ForEach",
                "dependsOn": [
                    {
                        "activity": "Set Companies Array",
                        "dependencyConditions": [
                            "Succeeded"
                        ]
                    }
                ],
                "userProperties": [],
                "typeProperties": {
                    "items": {
                        "value": "@variables('CompaniesArray')",
                        "type": "Expression"
                    },
                    "isSequential": true,
                    "activities": [
                        {
                            "name": "Stored procedure1",
                            "type": "SqlServerStoredProcedure",
                            "dependsOn": [],
                            "policy": {
                                "timeout": "7.00:00:00",
                                "retry": 0,
                                "retryIntervalInSeconds": 30,
                                "secureOutput": false,
                                "secureInput": false
                            },
                            "userProperties": [],
                            "typeProperties": {
                                "storedProcedureName": {
                                    "value": "@if(pipeline().parameters.DeleteCompanies,'[dbo].[LSInsight$DeleteCompany]','[dbo].[LSInsight$InsertCompany]')",
                                    "type": "Expression"
                                },
                                "storedProcedureParameters": {
                                    "CompanyName": {
                                        "value": {
                                            "value": "@trim(item())",
                                            "type": "Expression"
                                        },
                                        "type": "String"
                                    }
                                }
                            },
                            "linkedServiceName": {
                                "referenceName": "LSInsightDW",
                                "type": "LinkedServiceReference"
                            }
                        }
                    ]
                }
            }
        ],
        "parameters": {
            "Companies": {
                "type": "string"
            },
            "DeleteCompanies": {
                "type": "bool",
                "defaultValue": false
            }
        },
        "variables": {
            "CompaniesArray": {
                "type": "Array"
            }
        },
        "folder": {
            "name": "1 - LS Insight query setup"
        },
        "annotations": [],
        "lastPublishTime": "2020-11-05T11:21:32Z"
    },
            "dependsOn": [
                "[concat(variables('factoryId'), '/datasets/LSInsightDW')]",
                "[concat(variables('factoryId'), '/linkedServices/LSInsightDW')]"
            ]
        },
        {
    "name": "[concat(parameters('factoryName'), '/Add or Delete App Affix')]",
	"type": "Microsoft.DataFactory/factories/pipelines",
    "apiVersion": "2018-06-01",
    "properties": {
        "activities": [
            {
                "name": "Run_LSInsight_EditPublisherAffixReg",
                "type": "SqlServerStoredProcedure",
                "dependsOn": [],
                "policy": {
                    "timeout": "7.00:00:00",
                    "retry": 0,
                    "retryIntervalInSeconds": 30,
                    "secureOutput": false,
                    "secureInput": false
                },
                "userProperties": [],
                "typeProperties": {
                    "storedProcedureName": "[[dbo].[LSInsight$EditPublisherAffixReg]",
                    "storedProcedureParameters": {
                        "AppIDString": {
                            "value": {
                                "value": "@pipeline().parameters.AppID",
                                "type": "Expression"
                            },
                            "type": "String"
                        },
                        "AppName": {
                            "value": {
                                "value": "@pipeline().parameters.AppName",
                                "type": "Expression"
                            },
                            "type": "String"
                        },
                        "DeleteApp": {
                            "value": {
                                "value": "@pipeline().parameters.DeleteApp",
                                "type": "Expression"
                            },
                            "type": "String"
                        },
                        "Prefix": {
                            "value": {
                                "value": "@pipeline().parameters.Prefix",
                                "type": "Expression"
                            },
                            "type": "String"
                        },
                        "Publisher": {
                            "value": {
                                "value": "@pipeline().parameters.Publisher",
                                "type": "Expression"
                            },
                            "type": "String"
                        },
                        "Suffix": {
                            "value": {
                                "value": "@pipeline().parameters.Suffix",
                                "type": "Expression"
                            },
                            "type": "String"
                        },
                        "AppID": {
                            "value": "00000000-0000-0000-0000-000000000000",
                            "type": "String"
                        }
                    }
                },
                "linkedServiceName": {
                    "referenceName": "LSInsightDW",
                    "type": "LinkedServiceReference"
                }
            }
        ],
        "parameters": {
            "AppID": {
                "type": "string"
            },
            "AppName": {
                "type": "string"
            },
            "Publisher": {
                "type": "string"
            },
            "Prefix": {
                "type": "string"
            },
            "Suffix": {
                "type": "string"
            },
            "DeleteApp": {
                "type": "string",
                "defaultValue": "FALSE"
            }
        },
        "folder": {
            "name": "1 - LS Insight query setup"
        },
        "annotations": []
    },
	"dependsOn": [
                "[concat(variables('factoryId'), '/datasets/LSInsightDW')]",
                "[concat(variables('factoryId'), '/linkedServices/LSInsightDW')]"
    ]
    },
    {
    "name": "[concat(parameters('factoryName'), '/Add or Delete ShortTableNameMap')]",
	"type": "Microsoft.DataFactory/factories/pipelines",
    "apiVersion": "2018-06-01",
    "properties": {
        "activities": [
            {
                "name": "Run_LSInsight_EditShortTableNameMap",
                "type": "SqlServerStoredProcedure",
                "dependsOn": [],
                "policy": {
                    "timeout": "7.00:00:00",
                    "retry": 0,
                    "retryIntervalInSeconds": 30,
                    "secureOutput": false,
                    "secureInput": false
                },
                "userProperties": [],
                "typeProperties": {
                    "storedProcedureName": "[[dbo].[LSInsight$EditShortTableNameMap]",
                    "storedProcedureParameters": {
                        "DeleteRow": {
                            "value": {
                                "value": "@pipeline().parameters.DeleteRow",
                                "type": "Expression"
                            },
                            "type": "String"
                        },
                        "NewTableName": {
                            "value": {
                                "value": "@pipeline().parameters.NewTableName",
                                "type": "Expression"
                            },
                            "type": "String"
                        },
                        "OrgTableName": {
                            "value": {
                                "value": "@pipeline().parameters.OrgTableName",
                                "type": "Expression"
                            },
                            "type": "String"
                        }
                    }
                },
                "linkedServiceName": {
                    "referenceName": "LSInsightDW",
                    "type": "LinkedServiceReference"    
                }
            }
        ],
        "parameters": {
            "DeleteRow": {
                "type": "string",
                "defaultValue": "FALSE"
            },
            "OrgTableName": {
                "type": "string"
            },
            "NewTableName": {
                "type": "string"
            }
        },
        "folder": {
            "name": "1 - LS Insight query setup"
        },
        "annotations": []
    },
	"dependsOn": [
                "[concat(variables('factoryId'), '/datasets/LSInsightDW')]",
                "[concat(variables('factoryId'), '/linkedServices/LSInsightDW')]"
    ]
    },
        {
            "name": "[concat(parameters('factoryName'), '/Add or Delete Source Tables')]",
            "type": "Microsoft.DataFactory/factories/pipelines",
            "apiVersion": "2018-06-01",
            "properties": {
        "description": "Pipeline to set the source tables to use in LS Insight staging tables",
        "activities": [
            {
                "name": "Set SourceTable Array",
                "type": "SetVariable",
                "dependsOn": [],
                "userProperties": [],
                "typeProperties": {
                    "variableName": "SourceTableArray",
                    "value": {
                        "value": "@split(replace(replace(replace(pipeline().parameters.SourceTables,' ,',','),' , ',','),', ',','),',')",
                        "type": "Expression"
                    }
                }
            },
            {
                "name": "ForEach Source Table",
                "type": "ForEach",
                "dependsOn": [
                    {
                        "activity": "Set SourceTable Array",
                        "dependencyConditions": [
                            "Succeeded"
                        ]
                    }
                ],
                "userProperties": [],
                "typeProperties": {
                    "items": {
                        "value": "@variables('SourceTableArray')",
                        "type": "Expression"
                    },
                    "isSequential": true,
                    "activities": [
                        {
                            "name": "Stored procedure1",
                            "type": "SqlServerStoredProcedure",
                            "dependsOn": [],
                            "policy": {
                                "timeout": "7.00:00:00",
                                "retry": 0,
                                "retryIntervalInSeconds": 30,
                                "secureOutput": false,
                                "secureInput": false
                            },
                            "userProperties": [],
                            "typeProperties": {
                                "storedProcedureName": {
                                    "value": "@if(pipeline().parameters.DeleteSourceTables,'[dbo].[LSInsight$DeleteSourceTable]','[dbo].[LSInsight$InsertSourceTable]')",
                                    "type": "Expression"
                                },
                                "storedProcedureParameters": {
                                    "SourceTable": {
                                        "value": {
                                            "value": "@trim(item())",
                                            "type": "Expression"
                                        },
                                        "type": "String"
                                    }
                                }
                            },
                            "linkedServiceName": {
                                "referenceName": "LSInsightDW",
                                "type": "LinkedServiceReference"
                                
                            }
                        }
                    ]
                }
            }
        ],
        "parameters": {
            "SourceTables": {
                "type": "string"
            },
            "DeleteSourceTables": {
                "type": "bool",
                "defaultValue": false
            }
        },
        "variables": {
            "SourceTableArray": {
                "type": "Array"
            }
        },
        "folder": {
            "name": "1 - LS Insight query setup"
        },
        "annotations": [],
        "lastPublishTime": "2020-11-05T11:21:32Z"
    },
            "dependsOn": [
                "[concat(variables('factoryId'), '/datasets/LSInsightDW')]",
                "[concat(variables('factoryId'), '/linkedServices/LSInsightDW')]"

            ]
        },		
		{
            "name": "[concat(parameters('factoryName'), '/PopulateQueryBase')]",
            "type": "Microsoft.DataFactory/factories/pipelines",
            "apiVersion": "2018-06-01",
            "properties": {
        "description": "Extract information from [INFORMATION_SCHEMA].[COLUMNS] to populate all needed Select queries and Drop and Create for all destination tables",
        "activities": [
            {
                "name": "If true populate",
                "description": "If PopulateQueryBase = true then populate query base, ELSE do nothing",
                "type": "IfCondition",
                "dependsOn": [
                    {
                        "activity": "If true Get Metadata",
                        "dependencyConditions": [
                            "Succeeded"
                        ]
                    }
                ],
                "userProperties": [],
                "typeProperties": {
                    "expression": {
                        "value": "@equals(pipeline().parameters.PopulateQueryBase,true)",
                        "type": "Expression"
                    },
                    "ifTrueActivities": [
                        {
                            "name": "PopulateQueryBase",
                            "type": "Copy",
                            "dependsOn": [],
                            "policy": {
                                "timeout": "7.00:00:00",
                                "retry": 0,
                                "retryIntervalInSeconds": 30,
                                "secureOutput": false,
                                "secureInput": false
                            },
                            "userProperties": [],
                            "typeProperties": {
                                "source": {
                                    "type": "AzureSqlSource",
                                    "sqlReaderQuery": "DROP TABLE IF EXISTS #TEMPLSCentralColumnMetadata;\n\nWITH ShortTableName\nAS\n(SELECT\n		TRANSLATE([LongTableName], '.\"\\/%][', '_______') AS [LongTableName]\n	   ,TRANSLATE([Prefixed ShortTableName], '.\"\\/%][', '_______') AS [Prefixed ShortTableName]\n	FROM [LSInsight$ShortTableNameMap]\n	GROUP BY [LongTableName]\n	,[Prefixed ShortTableName]),\nPublisherAffixReg\nAS\n(SELECT\n	CAST([App ID] AS NVARCHAR(128)) AS [App ID]\n	,[Prefix]\n	,[Suffix]\n	FROM [dbo].[LSInsight$PublisherAffixReg]\n	UNION\n	SELECT\n	'N/A' AS [App ID]\n	,NULL AS [Prefix]\n	,NULL AS [Suffix]),\nShortenedColumnNames\nAS\n(SELECT\n		LCM.[TABLE_NAME]\n	 ,LCM.[COLUMN_NAME]\n	 ,LCM.[DATA_TYPE]\n	 ,LCM.[ORDINAL_POSITION]\n	 ,LCM.[CHARACTER_MAXIMUM_LENGTH]\n	 ,LCM.[NUMERIC_PRECISION]\n	 ,LCM.[NUMERIC_SCALE]\n	 ,LCM.[CountRows]\n	 ,COALESCE(STN.[LongTableName], LCM.[BaseTableName]) AS [BaseTableName]\n	 ,LCM.[TablePrefix]\n	 ,LCM.[ExtensionGUID]\n	 ,LCM.[Keycol]\n	 ,LCM.Publisher\n	FROM [LSInsight$LSCentralColumnMetadata] LCM\n	RIGHT HASH JOIN PublisherAffixReg PAR\n		ON LCM.[ExtensionGUID] = PAR.[App ID]\n	LEFT JOIN ShortTableName STN\n		ON LCM.BaseTableName = STN.[Prefixed ShortTableName]),\nCleanColumnNames\nAS\n(SELECT\n		SCN.[TABLE_NAME]\n	 ,SCN.[COLUMN_NAME] AS [Org_COLUMN_NAME]\n	 ,CASE\n			WHEN SCN.Publisher NOT IN ('Microsoft', 'Company') AND\n				LEFT(SCN.[COLUMN_NAME], LEN(PAR.Prefix)) = PAR.Prefix THEN TRIM(RIGHT(SCN.[COLUMN_NAME], LEN(SCN.[COLUMN_NAME]) - LEN(PAR.Prefix)))\n			WHEN SCN.Publisher NOT IN ('Microsoft', 'Company') AND\n				RIGHT(SCN.[COLUMN_NAME], LEN(PAR.Suffix)) = PAR.Suffix THEN TRIM(LEFT(SCN.[COLUMN_NAME], LEN(SCN.[COLUMN_NAME]) - LEN(PAR.Suffix)))\n			ELSE SCN.[COLUMN_NAME]\n		END AS [COLUMN_NAME]\n	 ,SCN.[DATA_TYPE]\n	 ,SCN.[ORDINAL_POSITION]\n	 ,SCN.[CHARACTER_MAXIMUM_LENGTH]\n	 ,SCN.[NUMERIC_PRECISION]\n	 ,SCN.[NUMERIC_SCALE]\n	 ,SCN.[CountRows]\n	 ,CASE\n			WHEN SCN.Publisher NOT IN ('Microsoft', 'Company') AND\n				LEFT(SCN.[BaseTableName], LEN(PAR.Prefix)) = PAR.Prefix THEN TRIM(RIGHT(SCN.[BaseTableName], LEN(SCN.[BaseTableName]) - LEN(PAR.Prefix)))\n			WHEN SCN.Publisher NOT IN ('Microsoft', 'Company') AND\n				RIGHT(SCN.[BaseTableName], LEN(PAR.Suffix)) = PAR.Suffix THEN TRIM(LEFT(SCN.[BaseTableName], LEN(SCN.[BaseTableName]) - LEN(PAR.Suffix)))\n			ELSE SCN.[BaseTableName]\n		END AS [BaseTableName]\n	 ,SCN.[BaseTableName] AS [Org_BaseTableName]\n	 ,SCN.[TablePrefix]\n	 ,SCN.[ExtensionGUID]\n	 ,SCN.[Keycol]\n	 ,SCN.Publisher\n	FROM ShortenedColumnNames SCN\n	LEFT JOIN PublisherAffixReg PAR\n		ON SCN.[ExtensionGUID] = PAR.[App ID]\n	LEFT JOIN ShortTableName STN\n		ON SCN.BaseTableName = STN.[Prefixed ShortTableName]),\nColumnOrder\nAS\n(SELECT\n		[TABLE_NAME]\n	 ,[Org_COLUMN_NAME]\n	 ,[COLUMN_NAME]\n	 ,CASE\n			WHEN Publisher NOT IN ('Microsoft', 'Company') AND\n				Keycol = 'FALSE' AND\n				COLUMN_NAME != 'timestamp' THEN DENSE_RANK() OVER (PARTITION BY [BaseTableName], [COLUMN_NAME] ORDER BY [ExtensionGUID])\n			ELSE NULL\n		END AS ColumnWithSameNameOrder\n	FROM CleanColumnNames)\nSELECT\n	cl.[TABLE_NAME]\n ,cl.Org_COLUMN_NAME\n ,CASE\n		WHEN co.ColumnWithSameNameOrder > 1 THEN cl.[COLUMN_NAME] + '-duplicate-' + CAST(co.ColumnWithSameNameOrder AS VARCHAR(2))\n		ELSE cl.[COLUMN_NAME]\n	END AS [COLUMN_NAME]\n ,cl.[DATA_TYPE]\n ,cl.[ORDINAL_POSITION]\n ,cl.[CHARACTER_MAXIMUM_LENGTH]\n ,cl.[NUMERIC_PRECISION]\n ,cl.[NUMERIC_SCALE]\n ,cl.[CountRows]\n ,cl.[BaseTableName]\n ,cl.[Org_BaseTableName]\n ,cl.[TablePrefix]\n ,cl.[ExtensionGUID]\n ,cl.[Keycol] INTO #TEMPLSCentralColumnMetadata\nFROM CleanColumnNames cl\n	,ColumnOrder co\nWHERE cl.TABLE_NAME = co.TABLE_NAME\nAND cl.Org_COLUMN_NAME = co.Org_COLUMN_NAME\n;\nWITH FilteredTableColumns\nAS\n(SELECT\n		TablePrefix,BaseTableName,CountRows,Keycol,COLUMN_NAME,Org_COLUMN_NAME,ExtensionGUID,[TABLE_NAME],ORDINAL_POSITION,DATA_TYPE,NUMERIC_SCALE,NUMERIC_PRECISION,CHARACTER_MAXIMUM_LENGTH\n	 ,REPLACE(BaseTableName, ' ', '') + CAST(DENSE_RANK() OVER (PARTITION BY BaseTableName ORDER BY ExtensionGUID) AS VARCHAR(3)) AS Extno\n	 ,DENSE_RANK() OVER (PARTITION BY BaseTableName, COLUMN_NAME ORDER BY ORDINAL_POSITION, ExtensionGUID) columntouse\n	FROM #TEMPLSCentralColumnMetadata AS AllTableColumns\n	inner Join [LSInsight$Companies] comp\n	ON comp.CompanyPrefix = AllTableColumns.TablePrefix\n	inner join [LSInsight$SourceTables] tabl\n	ON tabl.SourceTableName = AllTableColumns.BaseTableName\n	where comp.IncludeCompany = 'TRUE'\n	AND tabl.IncludeTable = 'TRUE'\n	),\n\n-- Get the ordinal position over all extensions\n-- Get a table number\n-- Cast decimal types to decimal(28,10) \n-- cast timestamp to bigint for incremental load\n-- create as type for each column type\n-- Create drop table statement\n-- Create destination Table name\ntt\nAS\n(SELECT --DISTINCT\n		FilteredTableColumns.TablePrefix\n	 ,FilteredTableColumns.BaseTableName\n	 ,FilteredTableColumns.Extno\n	 ,FilteredTableColumns.CountRows\n	 ,DENSE_RANK() OVER (PARTITION BY FilteredTableColumns.BaseTableName ORDER BY FilteredTableColumns.ExtensionGUID, FilteredTableColumns.ORDINAL_POSITION) extOrdinalpos\n	 ,FilteredTableColumns.Keycol\n	 ,CASE FilteredTableColumns.Keycol\n			WHEN 'TRUE' THEN FilteredTableColumns.Extno + '.[' + FilteredTableColumns.COLUMN_NAME + ']'\n			ELSE ''\n		END AS KeyJoinColumn\n	 ,FilteredTableColumns.ExtensionGUID\n	 ,CAST(FilteredTableColumns.[TABLE_NAME] AS NVARCHAR(200)) AS TableName\n		,FilteredTableColumns.COLUMN_NAME\n		,FilteredTableColumns.Org_COLUMN_NAME\n	 ,DENSE_RANK() OVER (ORDER BY FilteredTableColumns.TABLE_NAME) AS TableNo\n	 ,FilteredTableColumns.ORDINAL_POSITION\n ,CASE FilteredTableColumns.DATA_TYPE\n			WHEN 'decimal' THEN ' CAST(' + FilteredTableColumns.Extno + '.[' + FilteredTableColumns.Org_COLUMN_NAME + '] AS decimal(28,10)) AS [' + FilteredTableColumns.COLUMN_NAME + ']'\n			WHEN 'timestamp' THEN ' CAST(' + FilteredTableColumns.Extno + '.[' + FilteredTableColumns.Org_COLUMN_NAME + '] AS bigint) AS [bigint_' + FilteredTableColumns.COLUMN_NAME + ']'\n			ELSE FilteredTableColumns.Extno + '.[' + FilteredTableColumns.Org_COLUMN_NAME + '] AS [' + FilteredTableColumns.COLUMN_NAME + ']'\n		END AS caststatus\n		,CASE FilteredTableColumns.DATA_TYPE\n			WHEN 'timestamp' THEN ' CAST(' + FilteredTableColumns.Extno + '.[' + FilteredTableColumns.COLUMN_NAME + '] AS bigint) '\n			ELSE NULL\n		END AS Incrementalbase\n	 ,CASE DATA_TYPE\n			WHEN 'nvarchar' THEN '[' + FilteredTableColumns.COLUMN_NAME + '] [' + FilteredTableColumns.DATA_TYPE + '] (' + CAST(FilteredTableColumns.CHARACTER_MAXIMUM_LENGTH AS VARCHAR(10)) + ')'\n			WHEN 'varchar' THEN '[' + FilteredTableColumns.COLUMN_NAME + '] [' + FilteredTableColumns.DATA_TYPE + '] (' + CAST(FilteredTableColumns.CHARACTER_MAXIMUM_LENGTH AS VARCHAR(10)) + ')'\n			WHEN 'int' THEN '[' + FilteredTableColumns.COLUMN_NAME + '] [' + FilteredTableColumns.DATA_TYPE + ']'\n			WHEN 'decimal' THEN '[' + FilteredTableColumns.COLUMN_NAME + '] [' + FilteredTableColumns.DATA_TYPE + '] (' + CAST(FilteredTableColumns.NUMERIC_PRECISION AS VARCHAR(10)) + ', ' + CAST(FilteredTableColumns.NUMERIC_SCALE AS VARCHAR(10)) + ')'\n			WHEN 'datetime' THEN '[' + FilteredTableColumns.COLUMN_NAME + '] [' + FilteredTableColumns.DATA_TYPE + ']'\n			WHEN 'timestamp' THEN '[bigint_' + FilteredTableColumns.COLUMN_NAME + '] [Bigint]'\n			WHEN 'uniqueidentifier' THEN '[' + FilteredTableColumns.COLUMN_NAME + '] [' + FilteredTableColumns.DATA_TYPE + ']'\n			ELSE '[' + FilteredTableColumns.COLUMN_NAME + '] [' + FilteredTableColumns.DATA_TYPE + ']'\n		END Createstatus\n	 ,'DROP TABLE [stg' + BaseTableName + ']' AS DropTableScript\n	 ,'[stg ' + BaseTableName + ']' AS DestTableName\n	FROM FilteredTableColumns\n	GROUP BY\n	FilteredTableColumns.TablePrefix\n	 ,FilteredTableColumns.BaseTableName\n	 ,FilteredTableColumns.Extno\n	 ,FilteredTableColumns.CountRows\n	 ,FilteredTableColumns.ExtensionGUID, FilteredTableColumns.ORDINAL_POSITION\n	 ,FilteredTableColumns.Keycol\n	 ,FilteredTableColumns.[TABLE_NAME]\n	 ,FilteredTableColumns.COLUMN_NAME\n	 ,FilteredTableColumns.Org_COLUMN_NAME\n	 ,FilteredTableColumns.DATA_TYPE\n	 ,FilteredTableColumns.CHARACTER_MAXIMUM_LENGTH\n	 ,FilteredTableColumns.NUMERIC_PRECISION\n	 ,FilteredTableColumns.NUMERIC_SCALE\n)\n\n\n-- Get the base list of the source tables \n,BaseTable\nAS\n(SELECT \n		BaseTableName\n	 ,'[' + TableName + '] ' + Extno AS TableName\n	 ,TableNo, TablePrefix, CountRows\n	FROM tt\n	GROUP BY\n	BaseTableName,TableName,Extno,TableNo, TablePrefix, CountRows)\n\n-- Get the Columns for each source table for the Select Statement\n, BaseColumns AS ( \nSELECT \n		BaseTableName\n		,CASE WHEN tt.COLUMN_NAME = 'timestamp' then MIN(Incrementalbase) ELSE NULL END as IncrementalKey\n	 ,MIN(caststatus) as caststatus\n	 ,MIN(TableNo) as TableNo\n	 ,MIN(ORDINAL_POSITION) as ORDINAL_POSITION\n	 , TablePrefix\n	FROM tt\n	GROUP BY\n	BaseTableName, TablePrefix, COLUMN_NAME\n)\n\n-- Get the Columns for each source table for the create statement\n	, BaseCreateColumns AS ( \nSELECT \n		BaseTableName, COLUMN_NAME\n	 ,MIN(Createstatus) as Createstatus\n	 ,MIN(TableNo) as TableNo\n	 ,MIN(ORDINAL_POSITION) as ORDINAL_POSITION\n	 , TablePrefix\n	FROM tt\n	GROUP BY\n	BaseTableName, TablePrefix, COLUMN_NAME\n)\n\n-- Get the Primary key columns for each source table for the join statement\n,BaseKeyColumns AS (\nSELECT\n		BaseTableName\n	 ,KeyJoinColumn\n	 ,TableNo, ORDINAL_POSITION, TablePrefix\n	FROM tt\n\n)\n-- get a list of the source tables in a string (all extension in one string)\n,Tablelists\nAS\n(SELECT\n		BaseTable.BaseTableName,max(CountRows) AS CountRows, TablePrefix \n	 ,STRING_AGG(CAST(BaseTable.TableName + ' WITH (NOLOCK)' AS NVARCHAR(MAX)), ' ,')\n	 Within GROUP (Order By TableNo asc ) AS TableSnippet\n	FROM BaseTable\n	GROUP BY BaseTable.BaseTableName, TablePrefix\n	)\n\n-- get a list of the columns for each extension in a string (all extension columns in one string)\n,Columnlists AS (\nSELECT\n		BaseColumns.BaseTableName, TablePrefix, max(IncrementalKey) IncrementalKey\n	 ,STRING_AGG(CAST(BaseColumns.caststatus AS NVARCHAR(MAX)), ' ,')\n	 Within GROUP (Order By TableNo, ORDINAL_POSITION asc )AS ColumnSnippet\n	FROM BaseColumns\n	GROUP BY BaseColumns.BaseTableName, TablePrefix\n\n)\n-- get a list of the JOIN columns for each extension in a string - alll extensions join the base table (all joins in one string for each base table)\n,JoinTablelist AS\n(SELECT\n	BaseTableName\n	,COLUMN_NAME\n	,REPLACE(BaseTableName, ' ', '') + '1.[' + COLUMN_NAME +']' as BaseJoinCol \n ,COUNT(KeyJoinColumn) as CountJoins\n ,TablePrefix\nFROM tt\nwhere Keycol = 'TRUE'\ngroup by \nBaseTableName\n	,COLUMN_NAME ,TablePrefix\n)\n--List the Primary Keys for each base table\n,KeyJoinListString AS(\nSELECT\n	JoinTablelist.BaseTableName\n ,JoinTablelist.BaseJoinCol + ' = ' + tt.KeyJoinColumn as KeyJoinString\n ,TableNo\n ,ORDINAL_POSITION\n ,JoinTablelist.TablePrefix\nFROM JoinTablelist\nINNER JOIN tt\nON \nJoinTablelist.BaseTableName = tt.BaseTableName\nand JoinTablelist.TablePrefix = tt.TablePrefix\nand JoinTablelist.COLUMN_NAME = tt.COLUMN_NAME\nand tt.KeyJoinColumn <> ''\n)\n-- get a list of the join statement for each extension in a string (all extension joins in one string)\n,Joinlists AS (\nSELECT\n		KeyJoinListString.BaseTableName, KeyJoinListString.TablePrefix\n	 ,'WHERE ' + STRING_AGG(CAST(KeyJoinListString.KeyJoinString AS NVARCHAR(MAX)), ' AND ')\n	 Within GROUP (Order By TableNo, ORDINAL_POSITION asc )AS JoinSnippet\n	FROM KeyJoinListString\n	GROUP BY KeyJoinListString.BaseTableName, KeyJoinListString.TablePrefix\n	)\n--Generate the Create table snippet\n,CreateTableList AS(\nSelect BaseTableName, TablePrefix\n,'CREATE TABLE [stg$'+ BaseTableName + '] ([CompanyPrefix] [nvarchar] (200), ' + STRING_AGG(CAST(Createstatus AS NVARCHAR(MAX))\n, ' ,') WITHIN GROUP (order by TableNo, ORDINAL_POSITION asc ) +' , [ADF Run GUID] [nvarchar](100) NULL) ON [PRIMARY]' AS CreateSnippet\nfrom BaseCreateColumns\nGroup by BaseTableName, TablePrefix\n)\n--Generate the Drop table snippet\n,DropTableList AS(\nSelect BaseTableName, TablePrefix\n,'IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(''stg$'+ BaseTableName + ''' ) AND type in (''U'')) DROP TABLE [stg$'+ BaseTableName + '] ' DropSnippet\nfrom BaseCreateColumns\nGroup by BaseTableName, TablePrefix\n)\n--Assemble the results in one result array\nSELECT \n	Tablelists.BaseTableName\n	,Tablelists.CountRows\n	,SUM(Tablelists.CountRows) OVER (Partition BY Tablelists.BaseTableName) as SumCountRows\n,COUNT(Tablelists.CountRows) OVER (Partition BY Tablelists.BaseTableName) as CountCompanies\n,RANK() OVER(Partition BY Tablelists.BaseTableName ORDER BY Tablelists.TablePrefix) AS CompanyNumber\n	,Tablelists.TablePrefix\n	, 'stg$'+ Tablelists.BaseTableName As DestinationTable\n ,ColumnSnippet\n ,TableSnippet\n ,JoinSnippet\n ,'SELECT ' + ColumnSnippet + ' FROM ' + TableSnippet + ' ' + JoinSnippet AS SelectStatement\n ,DropSnippet\n ,CreateSnippet\n ,DropSnippet + ' ' + CreateSnippet as DropAndCreate\n ,IncrementalKey\nFROM Tablelists \nJOIN Columnlists \nON\nColumnlists.BaseTableName = Tablelists.BaseTableName\nAND Columnlists.TablePrefix = Tablelists.TablePrefix\nJOIN Joinlists\nON Columnlists.BaseTableName = Joinlists.BaseTableName\nAND Columnlists.TablePrefix = Joinlists.TablePrefix\nJOIN CreateTableList\nON Columnlists.BaseTableName = CreateTableList.BaseTableName\nAND Columnlists.TablePrefix = CreateTableList.TablePrefix\nJOIN DropTableList\nON Columnlists.BaseTableName = DropTableList.BaseTableName\nAND Columnlists.TablePrefix = DropTableList.TablePrefix\n--CLEAN Temp table\nDROP TABLE IF EXISTS #TEMPLSCentralColumnMetadata;",
                                    "queryTimeout": "12:00:00",
                                    "partitionOption": "None"
                                },
                                "sink": {
                                    "type": "AzureSqlSink",
                                     "preCopyScript": "IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[LSInsight$QUERY BASE]') AND type in (N'U'))\nBEGIN\nTRUNCATE TABLE [LSInsight$QUERY BASE]\nEND\nELSE\nBEGIN\nCREATE TABLE [LSInsight$QUERY BASE](\n\t[BaseTableName] [nvarchar](200) NULL,\n\t[CountRows] [bigint] NULL,\n\t[SumCountRows] [bigint] NULL,\n\t[CountCompanies] [bigint] NULL,\n\t[CompanyNumber] [bigint] NULL,\n\t[TablePrefix] [nvarchar](200) NULL,\n\t[DestinationTable] [nvarchar](200) NULL,\n\t[ColumnSnippet] [nvarchar](max) NULL,\n\t[TableSnippet] [nvarchar](max) NULL,\n\t[JoinSnippet] [nvarchar](max) NULL,\n\t[SelectStatement] [nvarchar](max) NULL,\n\t[DropSnippet] [nvarchar](max) NULL,\n\t[CreateSnippet] [nvarchar](max) NULL,\n\t[DropAndCreate] [nvarchar](max) NULL,\n\t[IncrementalKey] [nvarchar](200) NULL\n) ON [Primary]\nEND",
                                    "disableMetricsCollection": false
                                },
                                "enableStaging": false
                            },
                            "inputs": [
                                {
                                    "referenceName": "LSInsightDW",
                                    "type": "DatasetReference",
                                    "parameters": {
                                        "DestTableName": "LSInsight$LSCentralColumnMetadata"
                                    }
                                }
                            ],
                            "outputs": [
                                {
                                    "referenceName": "LSInsightDW",
                                    "type": "DatasetReference",
                                    "parameters": {
                                        "DestTableName": "LSInsight$QUERY BASE"
                                    }
                                }
                            ]
                        }
                    ]
                }
            },
            {
                "name": "If true Get Metadata",
                "description": "If PopulateQueryBase = true then populate query base, ELSE do nothing",
                "type": "IfCondition",
                "dependsOn": [],
                "userProperties": [],
                "typeProperties": {
                    "expression": {
                        "value": "@equals(pipeline().parameters.GetLSCentralMetadata,true)",
                        "type": "Expression"
                    },
                    "ifTrueActivities": [
                        {
                            "name": "GETLSCentralColumnMetadata",
                            "description": "GET LSCentral Column Metadata and load to [LSInsight$LSCentralColumnMetadata] in the LS Insight database",
                            "type": "Copy",
                            "dependsOn": [],
                            "policy": {
                                "timeout": "7.00:00:00",
                                "retry": 0,
                                "retryIntervalInSeconds": 30,
                                "secureOutput": false,
                                "secureInput": false
                            },
                            "userProperties": [],
                            "typeProperties": {
                                "source": {
                                    "type": "SqlServerSource",
                                    "sqlReaderQuery": "IF   EXISTS (SELECT\n		[App ID]\n	FROM [dbo].[NAV App Installed App]\n	WHERE [App ID] = '5ECFC871-5D82-43F1-9C54-59685E82318D')\nBEGIN\n-- Get the Primary key for each table \nWITH keycolumn\nAS\n(SELECT\n		Col.TABLE_NAME\n	   ,Col.COLUMN_NAME\n	   ,Col.COLUMN_NAME + ' = ' + Col.COLUMN_NAME AS JoinOnCol\n	FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS Tab\n		,INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE Col\n	WHERE Col.CONSTRAINT_NAME = Tab.CONSTRAINT_NAME\n	AND Col.TABLE_NAME = Tab.TABLE_NAME\n	AND Tab.CONSTRAINT_TYPE = 'PRIMARY KEY'\n	AND CHARINDEX('$', REVERSE(Col.TABLE_NAME)) > 0)\n\n-- Get APP ID for Installed apps and add Company as a fixed value\n,\nExt\nAS\n(SELECT\n		CAST([App ID] AS NVARCHAR(200)) [App ID]\n	   ,Publisher\n	FROM [NAV App Installed App])\n\n--Get all columns and metadata for all tables\n-- Extract the table name excluding the company prefix and extension GUID\n-- Extract the Company name / table prefix\n-- Extract the Extension GUID\n-- Mark the Primary Keys\n,\nAllTableColumns\nAS\n(SELECT DISTINCT\n		col.TABLE_NAME\n	   ,col.COLUMN_NAME\n	   ,col.DATA_TYPE\n	   ,col.ORDINAL_POSITION\n	   ,col.CHARACTER_MAXIMUM_LENGTH\n	   ,col.NUMERIC_PRECISION\n	   ,col.NUMERIC_SCALE\n	   ,rowcounts.rows CountRows\n	   ,SUBSTRING(col.TABLE_NAME, CHARINDEX('$', col.TABLE_NAME) + 1, ABS(CHARINDEX('$', col.TABLE_NAME, CHARINDEX('$', col.TABLE_NAME) + 1) - CHARINDEX('$', col.TABLE_NAME) - 1))\n		AS BaseTableName\n	   ,LEFT(col.TABLE_NAME, ABS(CHARINDEX('$', col.TABLE_NAME) - 1))\n		AS TablePrefix\n	   ,UPPER(RIGHT(col.TABLE_NAME, CHARINDEX('$', REVERSE(col.TABLE_NAME)) - 1))\n		AS ExtensionGUID\n	   ,IIF(col.COLUMN_NAME = keycolumn.COLUMN_NAME, 'TRUE', 'FALSE') Keycol\n\n	FROM [INFORMATION_SCHEMA].[COLUMNS] col\n	LEFT JOIN keycolumn\n		ON col.TABLE_NAME = keycolumn.TABLE_NAME\n		AND col.COLUMN_NAME = keycolumn.COLUMN_NAME\n	LEFT JOIN (SELECT DISTINCT\n			p.rows\n		   ,t.name\n		FROM sys.partitions p\n			,sys.tables t\n		WHERE p.object_id = t.object_id) rowcounts\n		ON col.TABLE_NAME = rowcounts.name\n	WHERE (col.TABLE_NAME LIKE '_%$_%$_____%'\n	AND col.TABLE_NAME NOT LIKE '%$VSIFT$%'\n	)\n	GROUP BY col.TABLE_NAME\n			,col.COLUMN_NAME\n			,col.DATA_TYPE\n			,col.ORDINAL_POSITION\n			,col.CHARACTER_MAXIMUM_LENGTH\n			,col.NUMERIC_PRECISION\n			,col.NUMERIC_SCALE\n			,rowcounts.rows\n			,keycolumn.COLUMN_NAME)\n\nSELECT\n	ATC.[TABLE_NAME]\n   ,ATC.[COLUMN_NAME]\n   ,[DATA_TYPE]\n   ,[ORDINAL_POSITION]\n   ,[CHARACTER_MAXIMUM_LENGTH]\n   ,[NUMERIC_PRECISION]\n   ,[NUMERIC_SCALE]\n   ,[CountRows]\n   ,[BaseTableName]\n   ,[TablePrefix]\n   ,CAST([ExtensionGUID] AS NVARCHAR(200)) AS [ExtensionGUID]\n   ,[Keycol]\n   ,Ext.Publisher\nFROM AllTableColumns ATC\nLEFT JOIN Ext\n	ON ATC.ExtensionGUID = Ext.[App ID]\nEND\nELSE\n\n--For LS Central versions <= 14.2\nBEGIN\n\nWITH keycolumn\nAS\n(SELECT\n		Col.TABLE_NAME\n	   ,Col.COLUMN_NAME\n	   ,Col.COLUMN_NAME + ' = ' + Col.COLUMN_NAME AS JoinOnCol\n	FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS Tab\n		,INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE Col\n	WHERE Col.CONSTRAINT_NAME = Tab.CONSTRAINT_NAME\n	AND Col.TABLE_NAME = Tab.TABLE_NAME\n	AND Tab.CONSTRAINT_TYPE = 'PRIMARY KEY')\n\n--Get all columns and metadata for all tables\n-- Extract the table name excluding the company prefix and extension GUID\n-- Extract the Company name / table prefix\n-- Extract the Extension GUID\n-- Mark the Primary Keys\n,\nAllTableColumns\nAS\n(SELECT\n		col.TABLE_NAME\n	   ,col.COLUMN_NAME\n	   ,col.DATA_TYPE\n	   ,col.ORDINAL_POSITION\n	   ,col.CHARACTER_MAXIMUM_LENGTH\n	   ,col.NUMERIC_PRECISION\n	   ,col.NUMERIC_SCALE\n	   ,rowcounts.rows CountRows\n	   ,SUBSTRING(col.TABLE_NAME, CHARINDEX('$', col.TABLE_NAME) + 1, ABS(LEN(col.TABLE_NAME) - CHARINDEX('$', col.TABLE_NAME)))\n		AS BaseTableName\n	   ,LEFT(col.TABLE_NAME, ABS(CHARINDEX('$', col.TABLE_NAME) - 1))\n		AS TablePrefix\n	   ,'N/A' AS ExtensionGUID\n	   ,IIF(col.COLUMN_NAME = keycolumn.COLUMN_NAME, 'TRUE', 'FALSE') Keycol\n	FROM [INFORMATION_SCHEMA].[COLUMNS] col\n	LEFT JOIN keycolumn\n		ON col.TABLE_NAME = keycolumn.TABLE_NAME\n	LEFT JOIN (SELECT DISTINCT\n			p.rows\n		   ,t.name\n		FROM sys.partitions p\n			,sys.tables t\n		WHERE p.object_id = t.object_id) rowcounts\n		ON col.TABLE_NAME = rowcounts.name\n	WHERE (col.TABLE_NAME LIKE '_%$_%'\n	AND col.TABLE_NAME NOT LIKE '%$VSIFT$%'\n	)\n	GROUP BY col.TABLE_NAME\n			,col.COLUMN_NAME\n			,col.DATA_TYPE\n			,col.ORDINAL_POSITION\n			,col.CHARACTER_MAXIMUM_LENGTH\n			,col.NUMERIC_PRECISION\n			,col.NUMERIC_SCALE\n			,rowcounts.rows\n			,keycolumn.COLUMN_NAME)\n\nSELECT\n	[TABLE_NAME]\n   ,[COLUMN_NAME]\n   ,[DATA_TYPE]\n   ,[ORDINAL_POSITION]\n   ,[CHARACTER_MAXIMUM_LENGTH]\n   ,[NUMERIC_PRECISION]\n   ,[NUMERIC_SCALE]\n   ,[CountRows]\n   ,BaseTableName\n   ,[TablePrefix]\n   ,[ExtensionGUID]\n   ,[Keycol]\nFROM AllTableColumns\n\nEND ",
                                    "queryTimeout": "12:00:00",
                                    "partitionOption": "None"
                                },
                                "sink": {
                                    "type": "AzureSqlSink",
                                    "preCopyScript": "IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[LSInsight$LSCentralColumnMetadata]') AND type in (N'U'))\nBEGIN\nTRUNCATE TABLE [dbo].[LSInsight$LSCentralColumnMetadata]\nEND\nELSE\nBEGIN\nCREATE TABLE [dbo].[LSInsight$LSCentralColumnMetadata](\n\t[TABLE_NAME] [sysname] NOT NULL,\n\t[COLUMN_NAME] [sysname] NULL,\n\t[DATA_TYPE] [nvarchar](128) NULL,\n\t[ORDINAL_POSITION] [int] NULL,\n\t[CHARACTER_MAXIMUM_LENGTH] [int] NULL,\n\t[NUMERIC_PRECISION] [tinyint] NULL,\n\t[NUMERIC_SCALE] [int] NULL,\n\t[CountRows] [bigint] NULL,\n\t[BaseTableName] [nvarchar](128) NULL,\n\t[TablePrefix] [nvarchar](128) NULL,\n\t[ExtensionGUID] [nvarchar](128) NULL,\n\t[Keycol] [varchar](5) NOT NULL,\n\t[Publisher] [nvarchar](250) NULL\n) ON [PRIMARY]\nEND",
                                "disableMetricsCollection": false
                                },
                                "enableStaging": false
                            },
                            "inputs": [
                                {
                                    "referenceName": "SourceDataset",
                                    "type": "DatasetReference",
                                    "parameters": {
                                        "dest_table": "NA"
                                    }
                                }
                            ],
                            "outputs": [
                                {
                                    "referenceName": "LSCentralColumnMetadata",
                                    "type": "DatasetReference"
                                }
                            ]
                        }
                    ]
                }
            }
        ],
        "concurrency": 1,
        "parameters": {
            "PopulateQueryBase": {
                "type": "bool",
                "defaultValue": false
            },
            "GetLSCentralMetadata": {
                "type": "bool",
                "defaultValue": false
            }
        },
        "folder": {
            "name": "1 - LS Insight query setup"
        },
        "annotations": [],
        "lastPublishTime": "2020-10-30T09:53:32Z"
    },
            "dependsOn": [
                "[concat(variables('factoryId'), '/datasets/LSInsightDW')]",
                "[concat(variables('factoryId'), '/datasets/SourceDataset')]",
                "[concat(variables('factoryId'), '/datasets/LSCentralColumnMetadata')]",
                "[concat(variables('factoryId'), '/linkedServices/LSInsightDW')]",
				"[concat(variables('factoryId'), '/linkedServices/LSCentralSource')]"
            ]
        },
        {
            "name": "[concat(parameters('factoryName'), '/Reset LS Insight DW')]",
            "type": "Microsoft.DataFactory/factories/pipelines",
            "apiVersion": "2018-06-01",
            "properties": {
            "description": "Truncate or clean up all LS Insight tables in the Data warehouse. ",
            "activities": [
                {
                "name": "Reset LSInsight DW",
                "type": "SqlServerStoredProcedure",
                "dependsOn": [],
                "policy": {
                    "timeout": "7.00:00:00",
                    "retry": 0,
                    "retryIntervalInSeconds": 30,
                    "secureOutput": false,
                    "secureInput": false
                },
                "userProperties": [],
                "typeProperties": {
                    "storedProcedureName": "[[dbo].[LSInsight$ResetDW]",
                    "storedProcedureParameters": {
                        "CleanALL": {
                            "value": {
                                "value": "@pipeline().parameters.Clean_ALL_DW_Tables",
                                "type": "Expression"
                            },
                            "type": "String"
                        },
            "Cleandim": {
                "value": "FALSE",
                "type": "String"
            },
            "Cleanfact": {
                "value": "FALSE",
                "type": "String"
            },
            "Cleanstaging": {
                "value": "FALSE",
                "type": "String"
            },
            "cmd": {
                "value": "cmd Query",
                "type": "String"
            }
                    }
                },
                "linkedServiceName": {
                    "referenceName": "LSInsightDW",
                    "type": "LinkedServiceReference"
                }
            }
            ],
            "parameters": {
            "Clean_ALL_DW_Tables": {
                "type": "string",
                "defaultValue": "FALSE"
            }
            },
            "folder": {
            "name": "1 - LS Insight query setup"
            },
            "annotations": [],
            "lastPublishTime": "2021-09-14T14:26:10Z"
            },
            "dependsOn": [
                "[concat(variables('factoryId'), '/datasets/LSInsightDW')]",
                "[concat(variables('factoryId'), '/datasets/SourceDataset')]",
                "[concat(variables('factoryId'), '/datasets/LSCentralColumnMetadata')]",
                "[concat(variables('factoryId'), '/linkedServices/LSInsightDW')]",
				"[concat(variables('factoryId'), '/linkedServices/LSCentralSource')]"
            ]
        },
        {
            "name": "[concat(parameters('factoryName'), '/Execute Dimension Stored Procedures')]",
            "type": "Microsoft.DataFactory/factories/pipelines",
            "apiVersion": "2018-06-01",
            "properties": {
            "activities": [
                {
                "name": "Get dim SP",
                "type": "Lookup",
                "dependsOn": [
                    {
                        "activity": "Populate dCompany",
                        "dependencyConditions": [
                            "Succeeded"
                        ]
                    }
                ],
                "policy": {
                    "timeout": "7.00:00:00",
                    "retry": 0,
                    "retryIntervalInSeconds": 30,
                    "secureOutput": false,
                    "secureInput": false
                },
                "userProperties": [],
                "typeProperties": {
                    "source": {
                        "type": "AzureSqlSource",
                        "sqlReaderQuery": "SELECT SPECIFIC_NAME spName\n  FROM INFORMATION_SCHEMA.ROUTINES\n WHERE ROUTINE_TYPE = 'PROCEDURE'\n AND SPECIFIC_NAME like 'dim%'",
                        "queryTimeout": "02:00:00",
                        "partitionOption": "None"
                    },
                    "dataset": {
                        "referenceName": "LSInsightDW",
                        "type": "DatasetReference",
                        "parameters": {
                            "DestTableName": "NA"
                        }
                    },
                    "firstRowOnly": false
                }
                },
                {
                "name": "ForEach1",
                "type": "ForEach",
                "dependsOn": [
                    {
                        "activity": "Set varSPNames",
                        "dependencyConditions": [
                            "Succeeded"
                        ]
                    }
                ],
                "userProperties": [],
                "typeProperties": {
                    "items": {
                        "value": "@variables('spNames')",
                        "type": "Expression"
                    },
                    "activities": [
                        {
                            "name": "Excecute Stored procedures",
                            "type": "SqlServerStoredProcedure",
                            "dependsOn": [],
                            "policy": {
                                "timeout": "7.00:00:00",
                                "retry": 0,
                                "retryIntervalInSeconds": 30,
                                "secureOutput": false,
                                "secureInput": false
                            },
                            "userProperties": [],
                            "typeProperties": {
                                "storedProcedureName": {
                                    "value": "@concat('[dbo].[',item().spName,']')",
                                    "type": "Expression"
                                }
                            },
                            "linkedServiceName": {
                                "referenceName": "LSInsightDW",
                                "type": "LinkedServiceReference"
                            }
                        },
                        {
                            "name": "UpdateLSInsightAudit",
                            "description": "Stored procedure activity to store the new Pipeline run metadata in LSAudit table",
                            "type": "SqlServerStoredProcedure",
                            "dependsOn": [
                                {
                                    "activity": "Excecute Stored procedures",
                                    "dependencyConditions": [
                                        "Succeeded"
                                    ]
                                }
                            ],
                            "policy": {
                                "timeout": "7.00:00:00",
                                "retry": 0,
                                "retryIntervalInSeconds": 30
                            },
                            "userProperties": [],
                            "typeProperties": {
                                "storedProcedureName": "[[dbo].[UpdateLSInsightAudit]",
                                "storedProcedureParameters": {
                                    "ADFRunGUID": {
                                        "value": {
                                            "value": "@pipeline().RunId",
                                            "type": "Expression"
                                        },
                                        "type": "String"
                                    },
                                    "CopyDuration": {
                                        "value": null,
                                        "type": "Int32"
                                    },
                                    "PipelineName": {
                                        "value": {
                                            "value": "@pipeline().Pipeline",
                                            "type": "Expression"
                                        },
                                        "type": "String"
                                    },
                                    "PipelineStart": {
                                        "value": {
                                            "value": "@utcnow()",
                                            "type": "Expression"
                                        },
                                        "type": "DateTime"
                                    },
                                    "RowsCopied": {
                                        "value": null,
                                        "type": "Int32"
                                    },
                                    "RowsRead": {
                                        "value": null,
                                        "type": "Int32"
                                    },
                                    "RowVersion": {
                                        "value": null,
                                        "type": "Int64"
                                    },
                                    "Table": {
                                        "value": {
                                            "value": "@item().spName",
                                            "type": "Expression"
                                        },
                                        "type": "String"
                                    },
                                    "PipelineTriggerType": {
                                        "value": {
                                            "value": "@pipeline().TriggerType",
                                            "type": "Expression"
                                        },
                                        "type": "String"
                                    },
                                    "ErrorMessage": {
                                        "value": null,
                                        "type": "String"
                                    }
                                }
                            },
                            "linkedServiceName": {
                                "referenceName": "LSInsightDW",
                                "type": "LinkedServiceReference"
                            }
                        },
                        {
                            "name": "Update LSInsightAudit On Error",
                            "description": "Stored procedure activity to store the new Pipeline run metadata in LSAudit table",
                            "type": "SqlServerStoredProcedure",
                            "dependsOn": [
                                {
                                    "activity": "Excecute Stored procedures",
                                    "dependencyConditions": [
                                        "Failed"
                                    ]
                                }
                            ],
                            "policy": {
                                "timeout": "7.00:00:00",
                                "retry": 0,
                                "retryIntervalInSeconds": 30
                            },
                            "userProperties": [],
                            "typeProperties": {
                                "storedProcedureName": "[[dbo].[UpdateLSInsightAudit]",
                                "storedProcedureParameters": {
                                    "ADFRunGUID": {
                                        "value": {
                                            "value": "@pipeline().RunId",
                                            "type": "Expression"
                                        },
                                        "type": "String"
                                    },
                                    "CopyDuration": {
                                        "value": {
                                            "value": "0",
                                            "type": "Expression"
                                        },
                                        "type": "Int32"
                                    },
                                    "PipelineName": {
                                        "value": {
                                            "value": "@concat('Failed Run - ',  pipeline().Pipeline)",
                                            "type": "Expression"
                                        },
                                        "type": "String"
                                    },
                                    "PipelineStart": {
                                        "value": {
                                            "value": "@utcnow()",
                                            "type": "Expression"
                                        },
                                        "type": "DateTime"
                                    },
                                    "RowsCopied": {
                                        "value": {
                                            "value": "0",
                                            "type": "Expression"
                                        },
                                        "type": "Int32"
                                    },
                                    "RowsRead": {
                                        "value": {
                                            "value": "0",
                                            "type": "Expression"
                                        },
                                        "type": "Int32"
                                    },
                                    "RowVersion": {
                                        "value": {
                                            "value": "0",
                                            "type": "Expression"
                                        },
                                        "type": "Int64"
                                    },
                                    "Table": {
                                        "value": {
                                            "value": "@item().spName",
                                            "type": "Expression"
                                        },
                                        "type": "String"
                                    },
                                    "PipelineTriggerType": {
                                        "value": {
                                            "value": "@pipeline().TriggerType",
                                            "type": "Expression"
                                        },
                                        "type": "String"
                                    },
                                    "ErrorMessage": {
                                        "value": {
                                            "value": "@activity('Excecute Stored procedures').error.message",
                                            "type": "Expression"
                                        },
                                        "type": "string"
                                    }
                                }
                            },
                            "linkedServiceName": {
                                "referenceName": "LSInsightDW",
                                "type": "LinkedServiceReference"
                            }
                        }
                    ]
                }
            },
            {
                "name": "Set varSPNames",
                "type": "SetVariable",
                "dependsOn": [
                    {
                        "activity": "Get dim SP",
                        "dependencyConditions": [
                            "Succeeded"
                        ]
                    }
                ],
                "userProperties": [],
                "typeProperties": {
                    "variableName": "spNames",
                    "value": {
                        "value": "@activity('Get dim SP').output.value",
                        "type": "Expression"
                    }
                }
            },
            {
                "name": "Populate dCompany",
                "type": "SqlServerStoredProcedure",
                "dependsOn": [],
                "policy": {
                    "timeout": "7.00:00:00",
                    "retry": 0,
                    "retryIntervalInSeconds": 30,
                    "secureOutput": false,
                    "secureInput": false
                },
                "userProperties": [],
                "typeProperties": {
                    "storedProcedureName": "[[dbo].[predimMergedCompany]"
                },
                "linkedServiceName": {
                    "referenceName": "LSInsightDW",
                    "type": "LinkedServiceReference"
                }
            }
        ],
                "variables": {
                    "spNames": {
                        "type": "Array"
                    }
                },
                "folder": {
                    "name": "3 - Dimension Tables"
                },
                "annotations": [],
                "lastPublishTime": "2020-11-17T16:43:33Z"
            },
            "dependsOn": [
                "[concat(variables('factoryId'), '/datasets/LSInsightDW')]",
                "[concat(variables('factoryId'), '/datasets/SourceDataset')]",
                "[concat(variables('factoryId'), '/datasets/LSCentralColumnMetadata')]",
                "[concat(variables('factoryId'), '/linkedServices/LSInsightDW')]",
				"[concat(variables('factoryId'), '/linkedServices/LSCentralSource')]"
            ]
        },
        {
            "name": "[concat(parameters('factoryName'), '/PL-SP-odMemberAttributes')]",
            "type": "Microsoft.DataFactory/factories/pipelines",
            "apiVersion": "2018-06-01",
            "properties": {
                "description": "Execute Stored Procedure in LS Insight DW\nNOTE - depends on DW.dMember",
                "activities": [
                    {
                        "name": "LookupLastRowVersion",
                        "description": "Retrieve  the max rowversion value stored in the control table",
                        "type": "Lookup",
                        "dependsOn": [],
                        "policy": {
                            "timeout": "7.00:00:00",
                            "retry": 0,
                            "retryIntervalInSeconds": 30
                        },
                        "userProperties": [],
                        "typeProperties": {
                            "source": {
                                "type": "AzureSqlSource",
                                "sqlReaderQuery": {
                                    "value": "@concat('SELECT max([NewRowVersion])  AS LastRowID \n  FROM ', pipeline().parameters.Control_Table_Table_Name, '\n  where [Table] =''',variables('DestinationTable'),'''')",
                                    "type": "Expression"
                                },
                                "partitionOption": "None"
                            },
                            "dataset": {
                                "referenceName": "LSInsightDW",
                                "type": "DatasetReference",
                                "parameters": {
                                    "DestTableName": "NA"
                                }
                            },
                            "firstRowOnly": true
                        }
                    },
                    {
                        "name": "LookupCurrentWaterMark",
                        "description": "Retrieve  the current maximum value in watermark column of source data store",
                        "type": "Lookup",
                        "dependsOn": [],
                        "policy": {
                            "timeout": "7.00:00:00",
                            "retry": 0,
                            "retryIntervalInSeconds": 30
                        },
                        "userProperties": [],
                        "typeProperties": {
                            "source": {
                                "type": "AzureSqlSource",
                                "sqlReaderQuery": {
                                    "value": "@concat('select COALESCE(MAX(convert(bigint, ',pipeline().parameters.Data_Source_WaterMarkColumn, ')),0) as NewWatermarkValue from ',variables('Data_Source_Table_Name'))",
                                    "type": "Expression"
                                },
                                "partitionOption": "None"
                            },
                            "dataset": {
                                "referenceName": "LSInsightDW",
                                "type": "DatasetReference",
                                "parameters": {
                                    "DestTableName": "NA"
                                }
                            }
                        }
                    },
                    {
                        "name": "UpdateLSInsightAudit",
                        "description": "Stored procedure activity to store the new Pipeline run metadata in LSAudit table",
                        "type": "SqlServerStoredProcedure",
                        "dependsOn": [
                            {
                                "activity": "Execute Stored Procedure",
                                "dependencyConditions": [
                                    "Succeeded"
                                ]
                            }
                        ],
                        "policy": {
                            "timeout": "7.00:00:00",
                            "retry": 0,
                            "retryIntervalInSeconds": 30
                        },
                        "userProperties": [],
                        "typeProperties": {
                            "storedProcedureName": "[[dbo].[UpdateLSInsightAudit]",
                            "storedProcedureParameters": {
                                "ADFRunGUID": {
                                    "value": {
                                        "value": "@pipeline().RunId",
                                        "type": "Expression"
                                    },
                                    "type": "String"
                                },
                                "CopyDuration": {
                                    "value": null,
                                    "type": "Int32"
                                },
                                "PipelineName": {
                                    "value": {
                                        "value": "@pipeline().Pipeline",
                                        "type": "Expression"
                                    },
                                    "type": "String"
                                },
                                "PipelineStart": {
                                    "value": {
                                        "value": "@variables('PipelineStart')",
                                        "type": "Expression"
                                    },
                                    "type": "DateTime"
                                },
                                "RowsCopied": {
                                    "value": {
                                        "value": "@activity('Execute Stored Procedure').output.firstRow.RowCount",
                                        "type": "Expression"
                                    },
                                    "type": "Int32"
                                },
                                "RowsRead": {
                                    "value": null,
                                    "type": "Int32"
                                },
                                "RowVersion": {
                                    "value": {
                                        "value": "@activity('LookupCurrentWaterMark').output.firstRow.NewWatermarkValue",
                                        "type": "Expression"
                                    },
                                    "type": "Int64"
                                },
                                "Table": {
                                    "value": {
                                        "value": "@variables('DestinationTable')",
                                        "type": "Expression"
                                    },
                                    "type": "String"
                                },
                                "PipelineTriggerType": {
                                    "value": {
                                        "value": "@pipeline().TriggerType",
                                        "type": "Expression"
                                    },
                                    "type": "String"
                                },
                                "ErrorMessage": {
                                    "value": null,
                                    "type": "String"
                                }
                            }
                        },
                        "linkedServiceName": {
                            "referenceName": "LSInsightDW",
                            "type": "LinkedServiceReference"
                        }
                    },
                    {
                        "name": "PipelineStart",
                        "type": "SetVariable",
                        "dependsOn": [],
                        "userProperties": [],
                        "typeProperties": {
                            "variableName": "PipelineStart",
                            "value": {
                                "value": "@utcnow()",
                                "type": "Expression"
                            }
                        }
                    },
                    {
                        "name": "SqlServerStoredProcedure1",
                        "description": "Stored procedure activity to store the new Pipeline run metadata in LSAudit table",
                        "type": "SqlServerStoredProcedure",
                        "dependsOn": [
                            {
                                "activity": "Execute Stored Procedure",
                                "dependencyConditions": [
                                    "Failed"
                                ]
                            }
                        ],
                        "policy": {
                            "timeout": "7.00:00:00",
                            "retry": 0,
                            "retryIntervalInSeconds": 30
                        },
                        "userProperties": [],
                        "typeProperties": {
                            "storedProcedureName": "[[dbo].[UpdateLSInsightAudit]",
                            "storedProcedureParameters": {
                                "ADFRunGUID": {
                                    "value": {
                                        "value": "@pipeline().RunId",
                                        "type": "Expression"
                                    },
                                    "type": "String"
                                },
                                "CopyDuration": {
                                    "value": {
                                        "value": "0",
                                        "type": "Expression"
                                    },
                                    "type": "Int32"
                                },
                                "PipelineName": {
                                    "value": {
                                        "value": "@concat('Failed Run - ',  pipeline().Pipeline)",
                                        "type": "Expression"
                                    },
                                    "type": "String"
                                },
                                "PipelineStart": {
                                    "value": {
                                        "value": "@variables('PipelineStart')",
                                        "type": "Expression"
                                    },
                                    "type": "DateTime"
                                },
                                "RowsCopied": {
                                    "value": {
                                        "value": "0",
                                        "type": "Expression"
                                    },
                                    "type": "Int32"
                                },
                                "RowsRead": {
                                    "value": {
                                        "value": "0",
                                        "type": "Expression"
                                    },
                                    "type": "Int32"
                                },
                                "RowVersion": {
                                    "value": {
                                        "value": "0",
                                        "type": "Expression"
                                    },
                                    "type": "Int64"
                                },
                                "Table": {
                                    "value": {
                                        "value": "@variables('Data_Source_Table_Name')",
                                        "type": "Expression"
                                    },
                                    "type": "String"
                                },
                                "PipelineTriggerType": {
                                    "value": {
                                        "value": "@pipeline().TriggerType",
                                        "type": "Expression"
                                    },
                                    "type": "String"
                                },
                                "ErrorMessage": {
                                    "value": {
                                        "value": "@activity('Execute Stored Procedure').error.message",
                                        "type": "Expression"
                                    },
                                    "type": "string"
                                }
                            }
                        },
                        "linkedServiceName": {
                            "referenceName": "LSInsightDW",
                            "type": "LinkedServiceReference"
                        }
                    },
                    {
                        "name": "SqlServerStoredProcedure2",
                        "description": "Stored procedure activity to store the new Pipeline run metadata in LSAudit table",
                        "type": "SqlServerStoredProcedure",
                        "dependsOn": [
                            {
                                "activity": "LookupCurrentWaterMark",
                                "dependencyConditions": [
                                    "Failed"
                                ]
                            }
                        ],
                        "policy": {
                            "timeout": "7.00:00:00",
                            "retry": 0,
                            "retryIntervalInSeconds": 30
                        },
                        "userProperties": [],
                        "typeProperties": {
                            "storedProcedureName": "[[dbo].[UpdateLSInsightAudit]",
                            "storedProcedureParameters": {
                                "ADFRunGUID": {
                                    "value": {
                                        "value": "@pipeline().RunId",
                                        "type": "Expression"
                                    },
                                    "type": "String"
                                },
                                "CopyDuration": {
                                    "value": {
                                        "value": "0",
                                        "type": "Expression"
                                    },
                                    "type": "Int32"
                                },
                                "PipelineName": {
                                    "value": {
                                        "value": "@concat('Failed Run - ',  pipeline().Pipeline)",
                                        "type": "Expression"
                                    },
                                    "type": "String"
                                },
                                "PipelineStart": {
                                    "value": {
                                        "value": "@variables('PipelineStart')",
                                        "type": "Expression"
                                    },
                                    "type": "DateTime"
                                },
                                "RowsCopied": {
                                    "value": {
                                        "value": "0",
                                        "type": "Expression"
                                    },
                                    "type": "Int32"
                                },
                                "RowsRead": {
                                    "value": {
                                        "value": "0",
                                        "type": "Expression"
                                    },
                                    "type": "Int32"
                                },
                                "RowVersion": {
                                    "value": {
                                        "value": "0",
                                        "type": "Expression"
                                    },
                                    "type": "Int64"
                                },
                                "Table": {
                                    "value": {
                                        "value": "@variables('DestinationTable')",
                                        "type": "Expression"
                                    },
                                    "type": "String"
                                },
                                "PipelineTriggerType": {
                                    "value": {
                                        "value": "@pipeline().TriggerType",
                                        "type": "Expression"
                                    },
                                    "type": "String"
                                },
                                "ErrorMessage": {
                                    "value": {
                                        "value": "@activity('LookupCurrentWaterMark').Error.Message",
                                        "type": "Expression"
                                    },
                                    "type": "string"
                                }
                            }
                        },
                        "linkedServiceName": {
                            "referenceName": "LSInsightDW",
                            "type": "LinkedServiceReference"
                        }
                    },
                    {
                        "name": "SqlServerStoredProcedure3",
                        "description": "Stored procedure activity to store the new Pipeline run metadata in LSAudit table",
                        "type": "SqlServerStoredProcedure",
                        "dependsOn": [
                            {
                                "activity": "LookupLastRowVersion",
                                "dependencyConditions": [
                                    "Failed"
                                ]
                            }
                        ],
                        "policy": {
                            "timeout": "7.00:00:00",
                            "retry": 0,
                            "retryIntervalInSeconds": 30
                        },
                        "userProperties": [],
                        "typeProperties": {
                            "storedProcedureName": "[[dbo].[UpdateLSInsightAudit]",
                            "storedProcedureParameters": {
                                "ADFRunGUID": {
                                    "value": {
                                        "value": "@pipeline().RunId",
                                        "type": "Expression"
                                    },
                                    "type": "String"
                                },
                                "CopyDuration": {
                                    "value": {
                                        "value": "0",
                                        "type": "Expression"
                                    },
                                    "type": "Int32"
                                },
                                "PipelineName": {
                                    "value": {
                                        "value": "@concat('Failed Run - ',  pipeline().Pipeline)",
                                        "type": "Expression"
                                    },
                                    "type": "String"
                                },
                                "PipelineStart": {
                                    "value": {
                                        "value": "@variables('PipelineStart')",
                                        "type": "Expression"
                                    },
                                    "type": "DateTime"
                                },
                                "RowsCopied": {
                                    "value": {
                                        "value": "0",
                                        "type": "Expression"
                                    },
                                    "type": "Int32"
                                },
                                "RowsRead": {
                                    "value": {
                                        "value": "0",
                                        "type": "Expression"
                                    },
                                    "type": "Int32"
                                },
                                "RowVersion": {
                                    "value": {
                                        "value": "0",
                                        "type": "Expression"
                                    },
                                    "type": "Int64"
                                },
                                "Table": {
                                    "value": {
                                        "value": "@variables('DestinationTable')",
                                        "type": "Expression"
                                    },
                                    "type": "String"
                                },
                                "PipelineTriggerType": {
                                    "value": {
                                        "value": "@pipeline().TriggerType",
                                        "type": "Expression"
                                    },
                                    "type": "String"
                                },
                                "ErrorMessage": {
                                    "value": {
                                        "value": "@activity('LookupLastRowVersion').Error.Message",
                                        "type": "Expression"
                                    },
                                    "type": "string"
                                }
                            }
                        },
                        "linkedServiceName": {
                            "referenceName": "LSInsightDW",
                            "type": "LinkedServiceReference"
                        }
                    },
                    {
                        "name": "Execute Stored Procedure",
                        "type": "Lookup",
                        "dependsOn": [
                            {
                                "activity": "LookupLastRowVersion",
                                "dependencyConditions": [
                                    "Succeeded"
                                ]
                            },
                            {
                                "activity": "PipelineStart",
                                "dependencyConditions": [
                                    "Succeeded"
                                ]
                            },
                            {
                                "activity": "LookupCurrentWaterMark",
                                "dependencyConditions": [
                                    "Succeeded"
                                ]
                            }
                        ],
                        "policy": {
                            "timeout": "7.00:00:00",
                            "retry": 0,
                            "retryIntervalInSeconds": 30,
                            "secureOutput": false,
                            "secureInput": false
                        },
                        "userProperties": [],
                        "typeProperties": {
                            "source": {
                                "type": "AzureSqlSource",
                                "sqlReaderStoredProcedureName": "[[dbo].[odimMergedMemberAttributes]",
                                "partitionOption": "None"
                            },
                            "dataset": {
                                "referenceName": "LSInsightDW",
                                "type": "DatasetReference",
                                "parameters": {
                                    "DestTableName": "NA"
                                }
                            }
                        }
                    }
                ],
                "parameters": {
                    "Data_Source_WaterMarkColumn": {
                        "type": "String",
                        "defaultValue": "[[bigint_timestamp]"
                    },
                    "Control_Table_Table_Name": {
                        "type": "String",
                        "defaultValue": "[[dbo].[LSInsightAudit]"
                    },
                    "Control_Table_Column_Name": {
                        "type": "String",
                        "defaultValue": "[[NewRowVersion]"
                    },
                    "TablePrefix": {
                        "type": "string",
                        "defaultValue": "[parameters('TablePrefix')]"
                    }
                },
                "variables": {
                    "PipelineStart": {
                        "type": "String"
                    },
                    "DestinationTable": {
                        "type": "String",
                        "defaultValue": "[[DW].[odMemberAttributes]"
                    },
                    "Data_Source_Table_Name": {
                        "type": "String",
                        "defaultValue": "[[Stg$Member Attribute]"
                    },
                    "ErrorMessage": {
                        "type": "String"
                    }
                },
                "folder": {
                    "name": "3 - Dimension Tables"
                },
                "annotations": []
            },
            "dependsOn": [
                "[concat(variables('factoryId'), '/linkedServices/LSInsightDW')]",
                "[concat(variables('factoryId'), '/datasets/LSInsightDW')]"
            ]
        },
        {
            "name": "[concat(parameters('factoryName'), '/PL-SP-FactSalesPosted')]",
            "type": "Microsoft.DataFactory/factories/pipelines",
            "apiVersion": "2018-06-01",
            "properties": {
                "description": "Execute Stored Procedure in LS Insight DW",
                "activities": [
                    {
                        "name": "Last-TSE-RowID",
                        "description": "Retrieve  the max rowversion value stored in the control table",
                        "type": "Lookup",
                        "dependsOn": [
                            {
                                "activity": "PipelineStart",
                                "dependencyConditions": [
                                    "Succeeded"
                                ]
                            }
                        ],
                        "policy": {
                            "timeout": "7.00:00:00",
                            "retry": 0,
                            "retryIntervalInSeconds": 30
                        },
                        "userProperties": [],
                        "typeProperties": {
                            "source": {
                                "type": "AzureSqlSource",
                                "sqlReaderQuery": {
                                    "value": "@concat('SELECT COALESCE(max([NewRowVersion]),0)  AS LastRowID \n  FROM ', pipeline().parameters.Control_Table_Table_Name, '\n  where [NOTE] = ''TSE'' and  [Table] =''',variables('DestinationTable'),'''')",
                                    "type": "Expression"
                                },
                                "partitionOption": "None"
                            },
                            "dataset": {
                                "referenceName": "LSInsightDW",
                                "type": "DatasetReference",
                                "parameters": {
                                    "DestTableName": "NA"
                                }
                            },
                            "firstRowOnly": true
                        }
                    },
                    {
                        "name": "UpdateLSInsightAudit-TSE",
                        "description": "Stored procedure activity to store the new Pipeline run metadata in LSAudit table",
                        "type": "SqlServerStoredProcedure",
                        "dependsOn": [
                            {
                                "activity": "Execute Stored Procedure",
                                "dependencyConditions": [
                                    "Succeeded"
                                ]
                            }
                        ],
                        "policy": {
                            "timeout": "7.00:00:00",
                            "retry": 0,
                            "retryIntervalInSeconds": 30
                        },
                        "userProperties": [],
                        "typeProperties": {
                            "storedProcedureName": "[[dbo].[UpdateLSInsightAudit]",
                            "storedProcedureParameters": {
                                "ADFRunGUID": {
                                    "value": {
                                        "value": "@pipeline().RunId",
                                        "type": "Expression"
                                    },
                                    "type": "String"
                                },
                                "CopyDuration": {
                                    "value": null,
                                    "type": "Int32"
                                },
                                "PipelineName": {
                                    "value": {
                                        "value": "@pipeline().Pipeline",
                                        "type": "Expression"
                                    },
                                    "type": "String"
                                },
                                "PipelineStart": {
                                    "value": {
                                        "value": "@variables('PipelineStart')",
                                        "type": "Expression"
                                    },
                                    "type": "DateTime"
                                },
                                "RowsCopied": {
                                    "value": {
                                        "value": "@activity('Execute Stored Procedure').output.firstRow.RowCount",
                                        "type": "Expression"
                                    },
                                    "type": "Int32"
                                },
                                "RowsRead": {
                                    "value": null,
                                    "type": "Int32"
                                },
                                "RowVersion": {
                                    "value": {
                                        "value": "@activity('Current-RowID-TSE').output.firstRow.NewRowID",
                                        "type": "Expression"
                                    },
                                    "type": "Int64"
                                },
                                "Table": {
                                    "value": {
                                        "value": "@variables('DestinationTable')",
                                        "type": "Expression"
                                    },
                                    "type": "String"
                                },
                                "PipelineTriggerType": {
                                    "value": {
                                        "value": "@pipeline().TriggerType",
                                        "type": "Expression"
                                    },
                                    "type": "String"
                                },
                                "ErrorMessage": {
                                    "value": null,
                                    "type": "String"
                                },
                                "Note": {
                                    "value": "TSE"
                                }
                            }
                        },
                        "linkedServiceName": {
                            "referenceName": "LSInsightDW",
                            "type": "LinkedServiceReference"
                        }
                    },
                    {
                        "name": "PipelineStart",
                        "type": "SetVariable",
                        "dependsOn": [],
                        "userProperties": [],
                        "typeProperties": {
                            "variableName": "PipelineStart",
                            "value": {
                                "value": "@utcnow()",
                                "type": "Expression"
                            }
                        }
                    },
                    {
                        "name": "SqlServerStoredProcedure1",
                        "description": "Stored procedure activity to store the new Pipeline run metadata in LSAudit table",
                        "type": "SqlServerStoredProcedure",
                        "dependsOn": [
                            {
                                "activity": "Execute Stored Procedure",
                                "dependencyConditions": [
                                    "Failed"
                                ]
                            }
                        ],
                        "policy": {
                            "timeout": "7.00:00:00",
                            "retry": 0,
                            "retryIntervalInSeconds": 30
                        },
                        "userProperties": [],
                        "typeProperties": {
                            "storedProcedureName": "[[dbo].[UpdateLSInsightAudit]",
                            "storedProcedureParameters": {
                                "ADFRunGUID": {
                                    "value": {
                                        "value": "@pipeline().RunId",
                                        "type": "Expression"
                                    },
                                    "type": "String"
                                },
                                "CopyDuration": {
                                    "value": {
                                        "value": "0",
                                        "type": "Expression"
                                    },
                                    "type": "Int32"
                                },
                                "PipelineName": {
                                    "value": {
                                        "value": "@concat('Failed Run - ',  pipeline().Pipeline)",
                                        "type": "Expression"
                                    },
                                    "type": "String"
                                },
                                "PipelineStart": {
                                    "value": {
                                        "value": "@variables('PipelineStart')",
                                        "type": "Expression"
                                    },
                                    "type": "DateTime"
                                },
                                "RowsCopied": {
                                    "value": {
                                        "value": "0",
                                        "type": "Expression"
                                    },
                                    "type": "Int32"
                                },
                                "RowsRead": {
                                    "value": {
                                        "value": "0",
                                        "type": "Expression"
                                    },
                                    "type": "Int32"
                                },
                                "RowVersion": {
                                    "value": {
                                        "value": "0",
                                        "type": "Expression"
                                    },
                                    "type": "Int64"
                                },
                                "Table": {
                                    "value": {
                                        "value": "@variables('Data_Source_Table_Name')",
                                        "type": "Expression"
                                    },
                                    "type": "String"
                                },
                                "PipelineTriggerType": {
                                    "value": {
                                        "value": "@pipeline().TriggerType",
                                        "type": "Expression"
                                    },
                                    "type": "String"
                                },
                                "ErrorMessage": {
                                    "value": {
                                        "value": "@activity('Execute Stored Procedure').error.message",
                                        "type": "Expression"
                                    },
                                    "type": "string"
                                }
                            }
                        },
                        "linkedServiceName": {
                            "referenceName": "LSInsightDW",
                            "type": "LinkedServiceReference"
                        }
                    },
                    {
                        "name": "Last-TSE-RowID-Error",
                        "description": "Stored procedure activity to store the new Pipeline run metadata in LSAudit table",
                        "type": "SqlServerStoredProcedure",
                        "dependsOn": [
                            {
                                "activity": "Last-TSE-RowID",
                                "dependencyConditions": [
                                    "Failed"
                                ]
                            }
                        ],
                        "policy": {
                            "timeout": "7.00:00:00",
                            "retry": 0,
                            "retryIntervalInSeconds": 30
                        },
                        "userProperties": [],
                        "typeProperties": {
                            "storedProcedureName": "[[dbo].[UpdateLSInsightAudit]",
                            "storedProcedureParameters": {
                                "ADFRunGUID": {
                                    "value": {
                                        "value": "@pipeline().RunId",
                                        "type": "Expression"
                                    },
                                    "type": "String"
                                },
                                "CopyDuration": {
                                    "value": {
                                        "value": "0",
                                        "type": "Expression"
                                    },
                                    "type": "Int32"
                                },
                                "PipelineName": {
                                    "value": {
                                        "value": "@concat('Failed Run - ',  pipeline().Pipeline)",
                                        "type": "Expression"
                                    },
                                    "type": "String"
                                },
                                "PipelineStart": {
                                    "value": {
                                        "value": "@variables('PipelineStart')",
                                        "type": "Expression"
                                    },
                                    "type": "DateTime"
                                },
                                "RowsCopied": {
                                    "value": {
                                        "value": "0",
                                        "type": "Expression"
                                    },
                                    "type": "Int32"
                                },
                                "RowsRead": {
                                    "value": {
                                        "value": "0",
                                        "type": "Expression"
                                    },
                                    "type": "Int32"
                                },
                                "RowVersion": {
                                    "value": {
                                        "value": "0",
                                        "type": "Expression"
                                    },
                                    "type": "Int64"
                                },
                                "Table": {
                                    "value": {
                                        "value": "@variables('DestinationTable')",
                                        "type": "Expression"
                                    },
                                    "type": "String"
                                },
                                "PipelineTriggerType": {
                                    "value": {
                                        "value": "@pipeline().TriggerType",
                                        "type": "Expression"
                                    },
                                    "type": "String"
                                },
                                "ErrorMessage": {
                                    "value": {
                                        "value": "@activity('Last-TSE-RowID').output.Error.Message",
                                        "type": "Expression"
                                    },
                                    "type": "string"
                                },
                                "Note": {
                                    "value": "TSE"
                                }
                            }
                        },
                        "linkedServiceName": {
                            "referenceName": "LSInsightDW",
                            "type": "LinkedServiceReference"
                        }
                    },
                    {
                        "name": "Execute Stored Procedure",
                        "type": "Lookup",
                        "dependsOn": [
                            {
                                "activity": "Last-TSE-RowID",
                                "dependencyConditions": [
                                    "Succeeded"
                                ]
                            },
                            {
                                "activity": "Last-SIL-RowID",
                                "dependencyConditions": [
                                    "Succeeded"
                                ]
                            },
                            {
                                "activity": "Last-SCM-RowID",
                                "dependencyConditions": [
                                    "Succeeded"
                                ]
                            },
                            {
                                "activity": "Current-RowID-TSE",
                                "dependencyConditions": [
                                    "Succeeded"
                                ]
                            },
                            {
                                "activity": "Current-RowID-SIL",
                                "dependencyConditions": [
                                    "Succeeded"
                                ]
                            },
                            {
                                "activity": "Current-RowID-SCM",
                                "dependencyConditions": [
                                    "Succeeded"
                                ]
                            }
                        ],
                        "policy": {
                            "timeout": "7.00:00:00",
                            "retry": 0,
                            "retryIntervalInSeconds": 30,
                            "secureOutput": false,
                            "secureInput": false
                        },
                        "userProperties": [],
                        "typeProperties": {
                            "source": {
                                "type": "AzureSqlSource",
                                "sqlReaderStoredProcedureName": "[[dbo].[factSalesPosted]",
                                "storedProcedureParameters": {
                                    "CreditMemolineRowID": {
                                        "type": "Int64",
                                        "value": {
                                            "value": "@activity('Last-SCM-RowID').output.firstRow.LastRowID",
                                            "type": "Expression"
                                        }
                                    },
                                    "IncoiveLineRowID": {
                                        "type": "Int64",
                                        "value": {
                                            "value": "@activity('Last-SIL-RowID').output.firstRow.LastRowID",
                                            "type": "Expression"
                                        }
                                    },
                                    "TransSalesEntryRowID": {
                                        "type": "Int64",
                                        "value": {
                                            "value": "@activity('Last-TSE-RowID').output.firstRow.LastRowID",
                                            "type": "Expression"
                                        }
                                    },
                                    "CreditMemolineNewRowID": {
                                        "type": "Int64",
                                        "value": {
                                            "value": "@activity('Current-RowID-SCM').output.firstRow.NewRowID",
                                            "type": "Expression"
                                        }
                                    },
                                    "IncoiveLineNewRowID": {
                                        "type": "Int64",
                                        "value": {
                                            "value": "@activity('Current-RowID-SIL').output.firstRow.NewRowID",
                                            "type": "Expression"
                                        }
                                    },
                                    "TransSalesEntryNewRowID": {
                                        "type": "Int64",
                                        "value": {
                                            "value": "@activity('Current-RowID-TSE').output.firstRow.NewRowID",
                                            "type": "Expression"
                                        }
                                    }
                                },
                                "queryTimeout": "23:00:00",
                                "partitionOption": "None"
                            },
                            "dataset": {
                                "referenceName": "LSInsightDW",
                                "type": "DatasetReference",
                                "parameters": {
                                    "DestTableName": "NA"
                                }
                            }
                        }
                    },
                    {
                        "name": "Last-SIL-RowID",
                        "description": "Retrieve  the max rowversion value stored in the control table",
                        "type": "Lookup",
                        "dependsOn": [
                            {
                                "activity": "PipelineStart",
                                "dependencyConditions": [
                                    "Succeeded"
                                ]
                            }
                        ],
                        "policy": {
                            "timeout": "7.00:00:00",
                            "retry": 0,
                            "retryIntervalInSeconds": 30
                        },
                        "userProperties": [],
                        "typeProperties": {
                            "source": {
                                "type": "AzureSqlSource",
                                "sqlReaderQuery": {
                                    "value": "@concat('SELECT COALESCE(max([NewRowVersion]),0)  AS LastRowID \n  FROM ', pipeline().parameters.Control_Table_Table_Name, '\n  where [NOTE] = ''SIL'' and  [Table] =''',variables('DestinationTable'),'''')",
                                    "type": "Expression"
                                },
                                "partitionOption": "None"
                            },
                            "dataset": {
                                "referenceName": "LSInsightDW",
                                "type": "DatasetReference",
                                "parameters": {
                                    "DestTableName": "NA"
                                }
                            },
                            "firstRowOnly": true
                        }
                    },
                    {
                        "name": "Last-SCM-RowID",
                        "description": "Retrieve  the max rowversion value stored in the control table",
                        "type": "Lookup",
                        "dependsOn": [
                            {
                                "activity": "PipelineStart",
                                "dependencyConditions": [
                                    "Succeeded"
                                ]
                            }
                        ],
                        "policy": {
                            "timeout": "7.00:00:00",
                            "retry": 0,
                            "retryIntervalInSeconds": 30
                        },
                        "userProperties": [],
                        "typeProperties": {
                            "source": {
                                "type": "AzureSqlSource",
                                "sqlReaderQuery": {
                                    "value": "@concat('SELECT COALESCE(max([NewRowVersion]),0)  AS LastRowID \n  FROM ', pipeline().parameters.Control_Table_Table_Name, '\n  where [NOTE] = ''TSE'' and  [Table] =''',variables('DestinationTable'),'''')",
                                    "type": "Expression"
                                },
                                "partitionOption": "None"
                            },
                            "dataset": {
                                "referenceName": "LSInsightDW",
                                "type": "DatasetReference",
                                "parameters": {
                                    "DestTableName": "NA"
                                }
                            },
                            "firstRowOnly": true
                        }
                    },
                    {
                        "name": "Last-SIL-RowID-Error",
                        "description": "Stored procedure activity to store the new Pipeline run metadata in LSAudit table",
                        "type": "SqlServerStoredProcedure",
                        "dependsOn": [
                            {
                                "activity": "Last-SIL-RowID",
                                "dependencyConditions": [
                                    "Failed"
                                ]
                            }
                        ],
                        "policy": {
                            "timeout": "7.00:00:00",
                            "retry": 0,
                            "retryIntervalInSeconds": 30
                        },
                        "userProperties": [],
                        "typeProperties": {
                            "storedProcedureName": "[[dbo].[UpdateLSInsightAudit]",
                            "storedProcedureParameters": {
                                "ADFRunGUID": {
                                    "value": {
                                        "value": "@pipeline().RunId",
                                        "type": "Expression"
                                    },
                                    "type": "String"
                                },
                                "CopyDuration": {
                                    "value": {
                                        "value": "0",
                                        "type": "Expression"
                                    },
                                    "type": "Int32"
                                },
                                "PipelineName": {
                                    "value": {
                                        "value": "@concat('Failed Run - ',  pipeline().Pipeline)",
                                        "type": "Expression"
                                    },
                                    "type": "String"
                                },
                                "PipelineStart": {
                                    "value": {
                                        "value": "@variables('PipelineStart')",
                                        "type": "Expression"
                                    },
                                    "type": "DateTime"
                                },
                                "RowsCopied": {
                                    "value": {
                                        "value": "0",
                                        "type": "Expression"
                                    },
                                    "type": "Int32"
                                },
                                "RowsRead": {
                                    "value": {
                                        "value": "0",
                                        "type": "Expression"
                                    },
                                    "type": "Int32"
                                },
                                "RowVersion": {
                                    "value": {
                                        "value": "0",
                                        "type": "Expression"
                                    },
                                    "type": "Int64"
                                },
                                "Table": {
                                    "value": {
                                        "value": "@variables('DestinationTable')",
                                        "type": "Expression"
                                    },
                                    "type": "String"
                                },
                                "PipelineTriggerType": {
                                    "value": {
                                        "value": "@pipeline().TriggerType",
                                        "type": "Expression"
                                    },
                                    "type": "String"
                                },
                                "ErrorMessage": {
                                    "value": {
                                        "value": "@activity('Last-SIL-RowID').output.Error.Message",
                                        "type": "Expression"
                                    },
                                    "type": "string"
                                },
                                "Note": {
                                    "value": "SIL"
                                }
                            }
                        },
                        "linkedServiceName": {
                            "referenceName": "LSInsightDW",
                            "type": "LinkedServiceReference"
                        }
                    },
                    {
                        "name": "Last-SCM-RowID-Error",
                        "description": "Stored procedure activity to store the new Pipeline run metadata in LSAudit table",
                        "type": "SqlServerStoredProcedure",
                        "dependsOn": [
                            {
                                "activity": "Last-SCM-RowID",
                                "dependencyConditions": [
                                    "Failed"
                                ]
                            }
                        ],
                        "policy": {
                            "timeout": "7.00:00:00",
                            "retry": 0,
                            "retryIntervalInSeconds": 30
                        },
                        "userProperties": [],
                        "typeProperties": {
                            "storedProcedureName": "[[dbo].[UpdateLSInsightAudit]",
                            "storedProcedureParameters": {
                                "ADFRunGUID": {
                                    "value": {
                                        "value": "@pipeline().RunId",
                                        "type": "Expression"
                                    },
                                    "type": "String"
                                },
                                "CopyDuration": {
                                    "value": {
                                        "value": "0",
                                        "type": "Expression"
                                    },
                                    "type": "Int32"
                                },
                                "PipelineName": {
                                    "value": {
                                        "value": "@concat('Failed Run - ',  pipeline().Pipeline)",
                                        "type": "Expression"
                                    },
                                    "type": "String"
                                },
                                "PipelineStart": {
                                    "value": {
                                        "value": "@variables('PipelineStart')",
                                        "type": "Expression"
                                    },
                                    "type": "DateTime"
                                },
                                "RowsCopied": {
                                    "value": {
                                        "value": "0",
                                        "type": "Expression"
                                    },
                                    "type": "Int32"
                                },
                                "RowsRead": {
                                    "value": {
                                        "value": "0",
                                        "type": "Expression"
                                    },
                                    "type": "Int32"
                                },
                                "RowVersion": {
                                    "value": {
                                        "value": "0",
                                        "type": "Expression"
                                    },
                                    "type": "Int64"
                                },
                                "Table": {
                                    "value": {
                                        "value": "@variables('DestinationTable')",
                                        "type": "Expression"
                                    },
                                    "type": "String"
                                },
                                "PipelineTriggerType": {
                                    "value": {
                                        "value": "@pipeline().TriggerType",
                                        "type": "Expression"
                                    },
                                    "type": "String"
                                },
                                "ErrorMessage": {
                                    "value": {
                                        "value": "@activity('Last-SCM-RowID').output.Error.Message",
                                        "type": "Expression"
                                    },
                                    "type": "string"
                                },
                                "Note": {
                                    "value": "SCM"
                                }
                            }
                        },
                        "linkedServiceName": {
                            "referenceName": "LSInsightDW",
                            "type": "LinkedServiceReference"
                        }
                    },
                    {
                        "name": "UpdateLSInsightAudit-SCM",
                        "description": "Stored procedure activity to store the new Pipeline run metadata in LSAudit table",
                        "type": "SqlServerStoredProcedure",
                        "dependsOn": [
                            {
                                "activity": "Execute Stored Procedure",
                                "dependencyConditions": [
                                    "Succeeded"
                                ]
                            }
                        ],
                        "policy": {
                            "timeout": "7.00:00:00",
                            "retry": 0,
                            "retryIntervalInSeconds": 30
                        },
                        "userProperties": [],
                        "typeProperties": {
                            "storedProcedureName": "[[dbo].[UpdateLSInsightAudit]",
                            "storedProcedureParameters": {
                                "ADFRunGUID": {
                                    "value": {
                                        "value": "@pipeline().RunId",
                                        "type": "Expression"
                                    },
                                    "type": "String"
                                },
                                "CopyDuration": {
                                    "value": null,
                                    "type": "Int32"
                                },
                                "PipelineName": {
                                    "value": {
                                        "value": "@pipeline().Pipeline",
                                        "type": "Expression"
                                    },
                                    "type": "String"
                                },
                                "PipelineStart": {
                                    "value": {
                                        "value": "@variables('PipelineStart')",
                                        "type": "Expression"
                                    },
                                    "type": "DateTime"
                                },
                                "RowsCopied": {
                                    "value": {
                                        "value": "@activity('Execute Stored Procedure').output.firstRow.RowCount",
                                        "type": "Expression"
                                    },
                                    "type": "Int32"
                                },
                                "RowsRead": {
                                    "value": null,
                                    "type": "Int32"
                                },
                                "RowVersion": {
                                    "value": {
                                        "value": "@activity('Current-RowID-SCM').output.firstRow.NewRowID",
                                        "type": "Expression"
                                    },
                                    "type": "Int64"
                                },
                                "Table": {
                                    "value": {
                                        "value": "@variables('DestinationTable')",
                                        "type": "Expression"
                                    },
                                    "type": "String"
                                },
                                "PipelineTriggerType": {
                                    "value": {
                                        "value": "@pipeline().TriggerType",
                                        "type": "Expression"
                                    },
                                    "type": "String"
                                },
                                "ErrorMessage": {
                                    "value": null,
                                    "type": "String"
                                },
                                "Note": {
                                    "value": "SCM"
                                }
                            }
                        },
                        "linkedServiceName": {
                            "referenceName": "LSInsightDW",
                            "type": "LinkedServiceReference"
                        }
                    },
                    {
                        "name": "UpdateLSInsightAudit-SIL",
                        "description": "Stored procedure activity to store the new Pipeline run metadata in LSAudit table",
                        "type": "SqlServerStoredProcedure",
                        "dependsOn": [
                            {
                                "activity": "Execute Stored Procedure",
                                "dependencyConditions": [
                                    "Succeeded"
                                ]
                            }
                        ],
                        "policy": {
                            "timeout": "7.00:00:00",
                            "retry": 0,
                            "retryIntervalInSeconds": 30
                        },
                        "userProperties": [],
                        "typeProperties": {
                            "storedProcedureName": "[[dbo].[UpdateLSInsightAudit]",
                            "storedProcedureParameters": {
                                "ADFRunGUID": {
                                    "value": {
                                        "value": "@pipeline().RunId",
                                        "type": "Expression"
                                    },
                                    "type": "String"
                                },
                                "CopyDuration": {
                                    "value": null,
                                    "type": "Int32"
                                },
                                "PipelineName": {
                                    "value": {
                                        "value": "@pipeline().Pipeline",
                                        "type": "Expression"
                                    },
                                    "type": "String"
                                },
                                "PipelineStart": {
                                    "value": {
                                        "value": "@variables('PipelineStart')",
                                        "type": "Expression"
                                    },
                                    "type": "DateTime"
                                },
                                "RowsCopied": {
                                    "value": {
                                        "value": "@activity('Execute Stored Procedure').output.firstRow.RowCount",
                                        "type": "Expression"
                                    },
                                    "type": "Int32"
                                },
                                "RowsRead": {
                                    "value": null,
                                    "type": "Int32"
                                },
                                "RowVersion": {
                                    "value": {
                                        "value": "@activity('Current-RowID-SIL').output.firstRow.NewRowID",
                                        "type": "Expression"
                                    },
                                    "type": "Int64"
                                },
                                "Table": {
                                    "value": {
                                        "value": "@variables('DestinationTable')",
                                        "type": "Expression"
                                    },
                                    "type": "String"
                                },
                                "PipelineTriggerType": {
                                    "value": {
                                        "value": "@pipeline().TriggerType",
                                        "type": "Expression"
                                    },
                                    "type": "String"
                                },
                                "ErrorMessage": {
                                    "value": null,
                                    "type": "String"
                                },
                                "Note": {
                                    "value": "SIL"
                                }
                            }
                        },
                        "linkedServiceName": {
                            "referenceName": "LSInsightDW",
                            "type": "LinkedServiceReference"
                        }
                    },
                    {
                        "name": "Current-RowID-TSE",
                        "description": "Retrieve  the current maximum value in watermark column of source data store",
                        "type": "Lookup",
                        "dependsOn": [],
                        "policy": {
                            "timeout": "7.00:00:00",
                            "retry": 0,
                            "retryIntervalInSeconds": 30
                        },
                        "userProperties": [],
                        "typeProperties": {
                            "source": {
                                "type": "AzureSqlSource",
                                "sqlReaderQuery": "SELECT COALESCE(max(bigint_timestamp), 0) as NewRowID  from [stg$Trans_ Sales Entry]",
                                "partitionOption": "None"
                            },
                            "dataset": {
                                "referenceName": "LSInsightDW",
                                "type": "DatasetReference",
                                "parameters": {
                                    "DestTableName": "NA"
                                }
                            }
                        }
                    },
                    {
                        "name": "Current-RowID-SCM",
                        "description": "Retrieve  the current maximum value in watermark column of source data store",
                        "type": "Lookup",
                        "dependsOn": [],
                        "policy": {
                            "timeout": "7.00:00:00",
                            "retry": 0,
                            "retryIntervalInSeconds": 30
                        },
                        "userProperties": [],
                        "typeProperties": {
                            "source": {
                                "type": "AzureSqlSource",
                                "sqlReaderQuery": "SELECT COALESCE(max(bigint_timestamp), 0)  as NewRowID from [stg$Sales Cr_Memo Line]",
                                "partitionOption": "None"
                            },
                            "dataset": {
                                "referenceName": "LSInsightDW",
                                "type": "DatasetReference",
                                "parameters": {
                                    "DestTableName": "NA"
                                }
                            }
                        }
                    },
                    {
                        "name": "Current-RowID-SIL",
                        "description": "Retrieve  the current maximum value in watermark column of source data store",
                        "type": "Lookup",
                        "dependsOn": [],
                        "policy": {
                            "timeout": "7.00:00:00",
                            "retry": 0,
                            "retryIntervalInSeconds": 30
                        },
                        "userProperties": [],
                        "typeProperties": {
                            "source": {
                                "type": "AzureSqlSource",
                                "sqlReaderQuery": "SELECT COALESCE(max(bigint_timestamp), 0) as NewRowID  from [stg$Sales Invoice Line]",
                                "partitionOption": "None"
                            },
                            "dataset": {
                                "referenceName": "LSInsightDW",
                                "type": "DatasetReference",
                                "parameters": {
                                    "DestTableName": "NA"
                                }
                            }
                        }
                    },
                    {
                        "name": "Current-RowID-TSE-Error",
                        "description": "Stored procedure activity to store the new Pipeline run metadata in LSAudit table",
                        "type": "SqlServerStoredProcedure",
                        "dependsOn": [
                            {
                                "activity": "Current-RowID-TSE",
                                "dependencyConditions": [
                                    "Failed"
                                ]
                            }
                        ],
                        "policy": {
                            "timeout": "7.00:00:00",
                            "retry": 0,
                            "retryIntervalInSeconds": 30
                        },
                        "userProperties": [],
                        "typeProperties": {
                            "storedProcedureName": "[[dbo].[UpdateLSInsightAudit]",
                            "storedProcedureParameters": {
                                "ADFRunGUID": {
                                    "value": {
                                        "value": "@pipeline().RunId",
                                        "type": "Expression"
                                    },
                                    "type": "String"
                                },
                                "CopyDuration": {
                                    "value": {
                                        "value": "0",
                                        "type": "Expression"
                                    },
                                    "type": "Int32"
                                },
                                "PipelineName": {
                                    "value": {
                                        "value": "@concat('Failed Run - ',  pipeline().Pipeline)",
                                        "type": "Expression"
                                    },
                                    "type": "String"
                                },
                                "PipelineStart": {
                                    "value": {
                                        "value": "@variables('PipelineStart')",
                                        "type": "Expression"
                                    },
                                    "type": "DateTime"
                                },
                                "RowsCopied": {
                                    "value": {
                                        "value": "0",
                                        "type": "Expression"
                                    },
                                    "type": "Int32"
                                },
                                "RowsRead": {
                                    "value": {
                                        "value": "0",
                                        "type": "Expression"
                                    },
                                    "type": "Int32"
                                },
                                "RowVersion": {
                                    "value": {
                                        "value": "0",
                                        "type": "Expression"
                                    },
                                    "type": "Int64"
                                },
                                "Table": {
                                    "value": {
                                        "value": "@variables('DestinationTable')",
                                        "type": "Expression"
                                    },
                                    "type": "String"
                                },
                                "PipelineTriggerType": {
                                    "value": {
                                        "value": "@pipeline().TriggerType",
                                        "type": "Expression"
                                    },
                                    "type": "String"
                                },
                                "ErrorMessage": {
                                    "value": {
                                        "value": "@activity('Current-RowID-TSE').output.Error.Message",
                                        "type": "Expression"
                                    },
                                    "type": "string"
                                },
                                "Note": {
                                    "value": "TSE"
                                }
                            }
                        },
                        "linkedServiceName": {
                            "referenceName": "LSInsightDW",
                            "type": "LinkedServiceReference"
                        }
                    },
                    {
                        "name": "Current-RowID-SIL-Error",
                        "description": "Stored procedure activity to store the new Pipeline run metadata in LSAudit table",
                        "type": "SqlServerStoredProcedure",
                        "dependsOn": [
                            {
                                "activity": "Current-RowID-SIL",
                                "dependencyConditions": [
                                    "Failed"
                                ]
                            }
                        ],
                        "policy": {
                            "timeout": "7.00:00:00",
                            "retry": 0,
                            "retryIntervalInSeconds": 30
                        },
                        "userProperties": [],
                        "typeProperties": {
                            "storedProcedureName": "[[dbo].[UpdateLSInsightAudit]",
                            "storedProcedureParameters": {
                                "ADFRunGUID": {
                                    "value": {
                                        "value": "@pipeline().RunId",
                                        "type": "Expression"
                                    },
                                    "type": "String"
                                },
                                "CopyDuration": {
                                    "value": {
                                        "value": "0",
                                        "type": "Expression"
                                    },
                                    "type": "Int32"
                                },
                                "PipelineName": {
                                    "value": {
                                        "value": "@concat('Failed Run - ',  pipeline().Pipeline)",
                                        "type": "Expression"
                                    },
                                    "type": "String"
                                },
                                "PipelineStart": {
                                    "value": {
                                        "value": "@variables('PipelineStart')",
                                        "type": "Expression"
                                    },
                                    "type": "DateTime"
                                },
                                "RowsCopied": {
                                    "value": {
                                        "value": "0",
                                        "type": "Expression"
                                    },
                                    "type": "Int32"
                                },
                                "RowsRead": {
                                    "value": {
                                        "value": "0",
                                        "type": "Expression"
                                    },
                                    "type": "Int32"
                                },
                                "RowVersion": {
                                    "value": {
                                        "value": "0",
                                        "type": "Expression"
                                    },
                                    "type": "Int64"
                                },
                                "Table": {
                                    "value": {
                                        "value": "@variables('DestinationTable')",
                                        "type": "Expression"
                                    },
                                    "type": "String"
                                },
                                "PipelineTriggerType": {
                                    "value": {
                                        "value": "@pipeline().TriggerType",
                                        "type": "Expression"
                                    },
                                    "type": "String"
                                },
                                "ErrorMessage": {
                                    "value": {
                                        "value": "@activity('Current-RowID-SIL').output.Error.Message",
                                        "type": "Expression"
                                    },
                                    "type": "string"
                                },
                                "Note": {
                                    "value": "SIL"
                                }
                            }
                        },
                        "linkedServiceName": {
                            "referenceName": "LSInsightDW",
                            "type": "LinkedServiceReference"
                        }
                    },
                    {
                        "name": "Current-RowID-SCM-Error",
                        "description": "Stored procedure activity to store the new Pipeline run metadata in LSAudit table",
                        "type": "SqlServerStoredProcedure",
                        "dependsOn": [
                            {
                                "activity": "Current-RowID-SCM",
                                "dependencyConditions": [
                                    "Failed"
                                ]
                            }
                        ],
                        "policy": {
                            "timeout": "7.00:00:00",
                            "retry": 0,
                            "retryIntervalInSeconds": 30
                        },
                        "userProperties": [],
                        "typeProperties": {
                            "storedProcedureName": "[[dbo].[UpdateLSInsightAudit]",
                            "storedProcedureParameters": {
                                "ADFRunGUID": {
                                    "value": {
                                        "value": "@pipeline().RunId",
                                        "type": "Expression"
                                    },
                                    "type": "String"
                                },
                                "CopyDuration": {
                                    "value": {
                                        "value": "0",
                                        "type": "Expression"
                                    },
                                    "type": "Int32"
                                },
                                "PipelineName": {
                                    "value": {
                                        "value": "@concat('Failed Run - ',  pipeline().Pipeline)",
                                        "type": "Expression"
                                    },
                                    "type": "String"
                                },
                                "PipelineStart": {
                                    "value": {
                                        "value": "@variables('PipelineStart')",
                                        "type": "Expression"
                                    },
                                    "type": "DateTime"
                                },
                                "RowsCopied": {
                                    "value": {
                                        "value": "0",
                                        "type": "Expression"
                                    },
                                    "type": "Int32"
                                },
                                "RowsRead": {
                                    "value": {
                                        "value": "0",
                                        "type": "Expression"
                                    },
                                    "type": "Int32"
                                },
                                "RowVersion": {
                                    "value": {
                                        "value": "0",
                                        "type": "Expression"
                                    },
                                    "type": "Int64"
                                },
                                "Table": {
                                    "value": {
                                        "value": "@variables('DestinationTable')",
                                        "type": "Expression"
                                    },
                                    "type": "String"
                                },
                                "PipelineTriggerType": {
                                    "value": {
                                        "value": "@pipeline().TriggerType",
                                        "type": "Expression"
                                    },
                                    "type": "String"
                                },
                                "ErrorMessage": {
                                    "value": {
                                        "value": "@activity('Current-RowID-SCM').output.Error.Message",
                                        "type": "Expression"
                                    },
                                    "type": "string"
                                },
                                "Note": {
                                    "value": "SCM"
                                }
                            }
                        },
                        "linkedServiceName": {
                            "referenceName": "LSInsightDW",
                            "type": "LinkedServiceReference"
                        }
                    }
                ],
                "parameters": {
                    "Data_Source_WaterMarkColumn": {
                        "type": "String",
                        "defaultValue": "[[RowID]"
                    },
                    "Control_Table_Table_Name": {
                        "type": "String",
                        "defaultValue": "[[dbo].[LSInsightAudit]"
                    },
                    "Control_Table_Column_Name": {
                        "type": "String",
                        "defaultValue": "[[NewRowVersion]"
                    },
                    "TablePrefix": {
                        "type": "string",
                        "defaultValue": "[parameters('TablePrefix')]"
                    }
                },
                "variables": {
                    "PipelineStart": {
                        "type": "String"
                    },
                    "DestinationTable": {
                        "type": "String",
                        "defaultValue": "[[DW].[FactSalesPosted]"
                    },
                    "Data_Source_Table_Name": {
                        "type": "String",
                        "defaultValue": "[[Stg$TransSalesEntry]"
                    },
                    "ErrorMessage": {
                        "type": "String"
                    }
                },
                "folder": {
                    "name": "4 - Fact Tables"
                },
                "annotations": []
            },
            "dependsOn": [
                "[concat(variables('factoryId'), '/linkedServices/LSInsightDW')]",
                "[concat(variables('factoryId'), '/datasets/LSInsightDW')]"
            ]
        },

        {
            "name": "[concat(parameters('factoryName'), '/PL-SP-factPurchaseReceipts')]",
            "type": "Microsoft.DataFactory/factories/pipelines",
            "apiVersion": "2018-06-01",
            "properties": {
                "description": "Execute Stored Procedure in LS Insight DW",
                "activities": [
            {
                "name": "Last-RowID",
                "description": "Retrieve  the max rowversion value stored in the control table",
                "type": "Lookup",
                "dependsOn": [
                    {
                        "activity": "PipelineStart",
                        "dependencyConditions": [
                            "Succeeded"
                        ]
                    }
                ],
                "policy": {
                    "timeout": "7.00:00:00",
                    "retry": 0,
                    "retryIntervalInSeconds": 30
                },
                "userProperties": [],
                "typeProperties": {
                    "source": {
                        "type": "AzureSqlSource",
                        "sqlReaderQuery": {
                            "value": "@concat('SELECT COALESCE(max([NewRowVersion]),0)  AS LastRowID \n  FROM ', pipeline().parameters.Control_Table_Table_Name, '\n  where [Table] =''',variables('DestinationTable'),'''')",
                            "type": "Expression"
                        },
                        "partitionOption": "None"
                    },
                    "dataset": {
                        "referenceName": "LSInsightDW",
                        "type": "DatasetReference",
                        "parameters": {
                            "DestTableName": "NA"
                        }
                    },
                    "firstRowOnly": true
                }
            },
            {
                "name": "UpdateLSInsightAudit",
                "description": "Stored procedure activity to store the new Pipeline run metadata in LSAudit table",
                "type": "SqlServerStoredProcedure",
                "dependsOn": [
                    {
                        "activity": "Execute Stored Procedure",
                        "dependencyConditions": [
                            "Succeeded"
                        ]
                    }
                ],
                "policy": {
                    "timeout": "7.00:00:00",
                    "retry": 0,
                    "retryIntervalInSeconds": 30
                },
                "userProperties": [],
                "typeProperties": {
                    "storedProcedureName": "[[dbo].[UpdateLSInsightAudit]",
                    "storedProcedureParameters": {
                        "ADFRunGUID": {
                            "value": {
                                "value": "@pipeline().RunId",
                                "type": "Expression"
                            },
                            "type": "String"
                        },
                        "CopyDuration": {
                            "value": null,
                            "type": "Int32"
                        },
                        "PipelineName": {
                            "value": {
                                "value": "@pipeline().Pipeline",
                                "type": "Expression"
                            },
                            "type": "String"
                        },
                        "PipelineStart": {
                            "value": {
                                "value": "@variables('PipelineStart')",
                                "type": "Expression"
                            },
                            "type": "DateTime"
                        },
                        "RowsCopied": {
                            "value": {
                                "value": "@activity('Execute Stored Procedure').output.firstRow.RowCount",
                                "type": "Expression"
                            },
                            "type": "Int32"
                        },
                        "RowsRead": {
                            "value": null,
                            "type": "Int32"
                        },
                        "RowVersion": {
                            "value": {
                                "value": "@activity('Current-RowID').output.firstRow.NewRowID",
                                "type": "Expression"
                            },
                            "type": "Int64"
                        },
                        "Table": {
                            "value": {
                                "value": "@variables('DestinationTable')",
                                "type": "Expression"
                            },
                            "type": "String"
                        },
                        "PipelineTriggerType": {
                            "value": {
                                "value": "@pipeline().TriggerType",
                                "type": "Expression"
                            },
                            "type": "String"
                        },
                        "ErrorMessage": {
                            "value": null,
                            "type": "String"
                        },
                        "Note": {
                            "value": null
                        }
                    }
                },
                "linkedServiceName": {
                    "referenceName": "LSInsightDW",
                    "type": "LinkedServiceReference"
                }
            },
            {
                "name": "PipelineStart",
                "type": "SetVariable",
                "dependsOn": [],
                "userProperties": [],
                "typeProperties": {
                    "variableName": "PipelineStart",
                    "value": {
                        "value": "@utcnow()",
                        "type": "Expression"
                    }
                }
            },
            {
                "name": "On Error Update Audit",
                "description": "Stored procedure activity to store the new Pipeline run metadata in LSAudit table",
                "type": "SqlServerStoredProcedure",
                "dependsOn": [
                    {
                        "activity": "Execute Stored Procedure",
                        "dependencyConditions": [
                            "Failed"
                        ]
                    }
                ],
                "policy": {
                    "timeout": "7.00:00:00",
                    "retry": 0,
                    "retryIntervalInSeconds": 30
                },
                "userProperties": [],
                "typeProperties": {
                    "storedProcedureName": "[[dbo].[UpdateLSInsightAudit]",
                    "storedProcedureParameters": {
                        "ADFRunGUID": {
                            "value": {
                                "value": "@pipeline().RunId",
                                "type": "Expression"
                            },
                            "type": "String"
                        },
                        "CopyDuration": {
                            "value": {
                                "value": "0",
                                "type": "Expression"
                            },
                            "type": "Int32"
                        },
                        "PipelineName": {
                            "value": {
                                "value": "@concat('Failed Run - ',  pipeline().Pipeline)",
                                "type": "Expression"
                            },
                            "type": "String"
                        },
                        "PipelineStart": {
                            "value": {
                                "value": "@variables('PipelineStart')",
                                "type": "Expression"
                            },
                            "type": "DateTime"
                        },
                        "RowsCopied": {
                            "value": {
                                "value": "0",
                                "type": "Expression"
                            },
                            "type": "Int32"
                        },
                        "RowsRead": {
                            "value": {
                                "value": "0",
                                "type": "Expression"
                            },
                            "type": "Int32"
                        },
                        "RowVersion": {
                            "value": {
                                "value": "0",
                                "type": "Expression"
                            },
                            "type": "Int64"
                        },
                        "Table": {
                            "value": {
                                "value": "@variables('Data_Source_Table_Name')",
                                "type": "Expression"
                            },
                            "type": "String"
                        },
                        "PipelineTriggerType": {
                            "value": {
                                "value": "@pipeline().TriggerType",
                                "type": "Expression"
                            },
                            "type": "String"
                        },
                        "ErrorMessage": {
                            "value": {
                                "value": "@activity('Execute Stored Procedure').error.message",
                                "type": "Expression"
                            },
                            "type": "string"
                        }
                    }
                },
                "linkedServiceName": {
                    "referenceName": "LSInsightDW",
                    "type": "LinkedServiceReference"
                }
            },
            {
                "name": "Last-RowID-Error",
                "description": "Stored procedure activity to store the new Pipeline run metadata in LSAudit table",
                "type": "SqlServerStoredProcedure",
                "dependsOn": [
                    {
                        "activity": "Last-RowID",
                        "dependencyConditions": [
                            "Failed"
                        ]
                    }
                ],
                "policy": {
                    "timeout": "7.00:00:00",
                    "retry": 0,
                    "retryIntervalInSeconds": 30
                },
                "userProperties": [],
                "typeProperties": {
                    "storedProcedureName": "[[dbo].[UpdateLSInsightAudit]",
                    "storedProcedureParameters": {
                        "ADFRunGUID": {
                            "value": {
                                "value": "@pipeline().RunId",
                                "type": "Expression"
                            },
                            "type": "String"
                        },
                        "CopyDuration": {
                            "value": {
                                "value": "0",
                                "type": "Expression"
                            },
                            "type": "Int32"
                        },
                        "PipelineName": {
                            "value": {
                                "value": "@concat('Failed Run - ',  pipeline().Pipeline)",
                                "type": "Expression"
                            },
                            "type": "String"
                        },
                        "PipelineStart": {
                            "value": {
                                "value": "@variables('PipelineStart')",
                                "type": "Expression"
                            },
                            "type": "DateTime"
                        },
                        "RowsCopied": {
                            "value": {
                                "value": "0",
                                "type": "Expression"
                            },
                            "type": "Int32"
                        },
                        "RowsRead": {
                            "value": {
                                "value": "0",
                                "type": "Expression"
                            },
                            "type": "Int32"
                        },
                        "RowVersion": {
                            "value": {
                                "value": "0",
                                "type": "Expression"
                            },
                            "type": "Int64"
                        },
                        "Table": {
                            "value": {
                                "value": "@variables('DestinationTable')",
                                "type": "Expression"
                            },
                            "type": "String"
                        },
                        "PipelineTriggerType": {
                            "value": {
                                "value": "@pipeline().TriggerType",
                                "type": "Expression"
                            },
                            "type": "String"
                        },
                        "ErrorMessage": {
                            "value": {
                                "value": "@activity('Last-RowID').output.Error.Message",
                                "type": "Expression"
                            },
                            "type": "string"
                        },
                        "Note": {
                            "value": ""
                        }
                    }
                },
                "linkedServiceName": {
                    "referenceName": "LSInsightDW",
                    "type": "LinkedServiceReference"
                }
            },
            {
                "name": "Execute Stored Procedure",
                "type": "Lookup",
                "dependsOn": [
                    {
                        "activity": "Last-RowID",
                        "dependencyConditions": [
                            "Succeeded"
                        ]
                    },
                    {
                        "activity": "Current-RowID",
                        "dependencyConditions": [
                            "Succeeded"
                        ]
                    }
                ],
                "policy": {
                    "timeout": "7.00:00:00",
                    "retry": 0,
                    "retryIntervalInSeconds": 30,
                    "secureOutput": false,
                    "secureInput": false
                },
                "userProperties": [],
                "typeProperties": {
                    "source": {
                        "type": "AzureSqlSource",
                        "sqlReaderStoredProcedureName": "[[dbo].[factPurchaseReceiptsExec]",
                        "storedProcedureParameters": {
                            "NewRowID": {
                                "type": "Int64",
                                "value": {
                                    "value": "@activity('Current-RowID').output.firstRow.NewRowID",
                                    "type": "Expression"
                                }
                            },
                            "CurrentRowID": {
                                "type": "Int64",
                                "value": {
                                    "value": "@activity('Last-RowID').output.firstRow.LastRowID",
                                    "type": "Expression"
                                }
                            }
                        },
                        "queryTimeout": "23:00:00",
                        "partitionOption": "None"
                    },
                    "dataset": {
                        "referenceName": "LSInsightDW",
                        "type": "DatasetReference",
                        "parameters": {
                            "DestTableName": "NA"
                        }
                    }
                }
            },
            {
                "name": "Current-RowID",
                "description": "Retrieve  the current maximum value in watermark column of source data store",
                "type": "Lookup",
                "dependsOn": [],
                "policy": {
                    "timeout": "7.00:00:00",
                    "retry": 0,
                    "retryIntervalInSeconds": 30
                },
                "userProperties": [],
                "typeProperties": {
                    "source": {
                        "type": "AzureSqlSource",
                        "sqlReaderQuery": {
                            "value": "@concat('IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(''',variables('Data_Source_Table_Name'),'''))  select MAX(convert(bigint, ',pipeline().parameters.Data_Source_WaterMarkColumn, ')) as NewRowID from ',variables('Data_Source_Table_Name'), ' ELSE Select 0 AS NewRowID ')",
                            "type": "Expression"
                        },
                        "partitionOption": "None"
                    },
                    "dataset": {
                        "referenceName": "LSInsightDW",
                        "type": "DatasetReference",
                        "parameters": {
                            "DestTableName": "NA"
                        }
                    }
                }
            },
            {
                "name": "Current-RowID-Error",
                "description": "Stored procedure activity to store the new Pipeline run metadata in LSAudit table",
                "type": "SqlServerStoredProcedure",
                "dependsOn": [
                    {
                        "activity": "Current-RowID",
                        "dependencyConditions": [
                            "Failed"
                        ]
                    }
                ],
                "policy": {
                    "timeout": "7.00:00:00",
                    "retry": 0,
                    "retryIntervalInSeconds": 30
                },
                "userProperties": [],
                "typeProperties": {
                    "storedProcedureName": "[[dbo].[UpdateLSInsightAudit]",
                    "storedProcedureParameters": {
                        "ADFRunGUID": {
                            "value": {
                                "value": "@pipeline().RunId",
                                "type": "Expression"
                            },
                            "type": "String"
                        },
                        "CopyDuration": {
                            "value": {
                                "value": "0",
                                "type": "Expression"
                            },
                            "type": "Int32"
                        },
                        "PipelineName": {
                            "value": {
                                "value": "@concat('Failed Run - ',  pipeline().Pipeline)",
                                "type": "Expression"
                            },
                            "type": "String"
                        },
                        "PipelineStart": {
                            "value": {
                                "value": "@variables('PipelineStart')",
                                "type": "Expression"
                            },
                            "type": "DateTime"
                        },
                        "RowsCopied": {
                            "value": {
                                "value": "0",
                                "type": "Expression"
                            },
                            "type": "Int32"
                        },
                        "RowsRead": {
                            "value": {
                                "value": "0",
                                "type": "Expression"
                            },
                            "type": "Int32"
                        },
                        "RowVersion": {
                            "value": {
                                "value": "0",
                                "type": "Expression"
                            },
                            "type": "Int64"
                        },
                        "Table": {
                            "value": {
                                "value": "@variables('DestinationTable')",
                                "type": "Expression"
                            },
                            "type": "String"
                        },
                        "PipelineTriggerType": {
                            "value": {
                                "value": "@pipeline().TriggerType",
                                "type": "Expression"
                            },
                            "type": "String"
                        },
                        "ErrorMessage": {
                            "value": {
                                "value": "@activity('Current-RowID').output.Error.Message",
                                "type": "Expression"
                            },
                            "type": "string"
                        },
                        "Note": {
                            "value": ""
                        }
                    }
                },
                "linkedServiceName": {
                    "referenceName": "LSInsightDW",
                    "type": "LinkedServiceReference"
                }
            }
        ],
                "parameters": {
                    "Data_Source_WaterMarkColumn": {
                        "type": "String",
                        "defaultValue": "[[bigint_timestamp]"
                    },
                    "Control_Table_Table_Name": {
                        "type": "String",
                        "defaultValue": "[[dbo].[LSInsightAudit]"
                    },
                    "Control_Table_Column_Name": {
                        "type": "String",
                        "defaultValue": "[[NewRowVersion]"
                    },
                    "TablePrefix": {
                        "type": "string",
                        "defaultValue": "[parameters('TablePrefix')]"
                    }
                },
                "variables": {
                    "PipelineStart": {
                        "type": "String"
                    },
                    "DestinationTable": {
                        "type": "String",
                        "defaultValue": "[[DW].[fPurchaseReceipts]"
                    },
                    "Data_Source_Table_Name": {
                        "type": "String",
                        "defaultValue": "[[stg$Purch_ Rcpt_ Line]"
                    },
                    "ErrorMessage": {
                        "type": "String"
                    }
                },
                "folder": {
                    "name": "4 - Fact Tables"
                },
                "annotations": []
           },
            "dependsOn": [
                "[concat(variables('factoryId'), '/linkedServices/LSInsightDW')]",
                "[concat(variables('factoryId'), '/datasets/LSInsightDW')]"  
            ]
        },
		
		{
            "name": "[concat(parameters('factoryName'), '/PL-SP-factPurchaseOrders')]",
            "type": "Microsoft.DataFactory/factories/pipelines",
            "apiVersion": "2018-06-01",
            "properties": {
                "description": "Execute Stored Procedure in LS Insight DW",
                "activities": [
            {
                "name": "Last-RowID",
                "description": "Retrieve  the max rowversion value stored in the control table",
                "type": "Lookup",
                "dependsOn": [
                    {
                        "activity": "PipelineStart",
                        "dependencyConditions": [
                            "Succeeded"
                        ]
                    }
                ],
                "policy": {
                    "timeout": "7.00:00:00",
                    "retry": 0,
                    "retryIntervalInSeconds": 30
                },
                "userProperties": [],
                "typeProperties": {
                    "source": {
                        "type": "AzureSqlSource",
                        "sqlReaderQuery": {
                            "value": "@concat('SELECT COALESCE(max([NewRowVersion]),0)  AS LastRowID \n  FROM ', pipeline().parameters.Control_Table_Table_Name, '\n  where [Table] =''',variables('DestinationTable'),'''')",
                            "type": "Expression"
                        },
                        "partitionOption": "None"
                    },
                    "dataset": {
                        "referenceName": "LSInsightDW",
                        "type": "DatasetReference",
                        "parameters": {
                            "DestTableName": "NA"
                        }
                    },
                    "firstRowOnly": true
                }
            },
            {
                "name": "UpdateLSInsightAudit",
                "description": "Stored procedure activity to store the new Pipeline run metadata in LSAudit table",
                "type": "SqlServerStoredProcedure",
                "dependsOn": [
                    {
                        "activity": "Execute Stored Procedure",
                        "dependencyConditions": [
                            "Succeeded"
                        ]
                    }
                ],
                "policy": {
                    "timeout": "7.00:00:00",
                    "retry": 0,
                    "retryIntervalInSeconds": 30
                },
                "userProperties": [],
                "typeProperties": {
                    "storedProcedureName": "[[dbo].[UpdateLSInsightAudit]",
                    "storedProcedureParameters": {
                        "ADFRunGUID": {
                            "value": {
                                "value": "@pipeline().RunId",
                                "type": "Expression"
                            },
                            "type": "String"
                        },
                        "CopyDuration": {
                            "value": null,
                            "type": "Int32"
                        },
                        "PipelineName": {
                            "value": {
                                "value": "@pipeline().Pipeline",
                                "type": "Expression"
                            },
                            "type": "String"
                        },
                        "PipelineStart": {
                            "value": {
                                "value": "@variables('PipelineStart')",
                                "type": "Expression"
                            },
                            "type": "DateTime"
                        },
                        "RowsCopied": {
                            "value": {
                                "value": "@activity('Execute Stored Procedure').output.firstRow.RowCount",
                                "type": "Expression"
                            },
                            "type": "Int32"
                        },
                        "RowsRead": {
                            "value": null,
                            "type": "Int32"
                        },
                        "RowVersion": {
                            "value": {
                                "value": "@activity('Current-RowID').output.firstRow.NewRowID",
                                "type": "Expression"
                            },
                            "type": "Int64"
                        },
                        "Table": {
                            "value": {
                                "value": "@variables('DestinationTable')",
                                "type": "Expression"
                            },
                            "type": "String"
                        },
                        "PipelineTriggerType": {
                            "value": {
                                "value": "@pipeline().TriggerType",
                                "type": "Expression"
                            },
                            "type": "String"
                        },
                        "ErrorMessage": {
                            "value": null,
                            "type": "String"
                        },
                        "Note": {
                            "value": null
                        }
                    }
                },
                "linkedServiceName": {
                    "referenceName": "LSInsightDW",
                    "type": "LinkedServiceReference"
                }
            },
            {
                "name": "PipelineStart",
                "type": "SetVariable",
                "dependsOn": [],
                "userProperties": [],
                "typeProperties": {
                    "variableName": "PipelineStart",
                    "value": {
                        "value": "@utcnow()",
                        "type": "Expression"
                    }
                }
            },
            {
                "name": "On Error Update Audit",
                "description": "Stored procedure activity to store the new Pipeline run metadata in LSAudit table",
                "type": "SqlServerStoredProcedure",
                "dependsOn": [
                    {
                        "activity": "Execute Stored Procedure",
                        "dependencyConditions": [
                            "Failed"
                        ]
                    }
                ],
                "policy": {
                    "timeout": "7.00:00:00",
                    "retry": 0,
                    "retryIntervalInSeconds": 30
                },
                "userProperties": [],
                "typeProperties": {
                    "storedProcedureName": "[[dbo].[UpdateLSInsightAudit]",
                    "storedProcedureParameters": {
                        "ADFRunGUID": {
                            "value": {
                                "value": "@pipeline().RunId",
                                "type": "Expression"
                            },
                            "type": "String"
                        },
                        "CopyDuration": {
                            "value": {
                                "value": "0",
                                "type": "Expression"
                            },
                            "type": "Int32"
                        },
                        "PipelineName": {
                            "value": {
                                "value": "@concat('Failed Run - ',  pipeline().Pipeline)",
                                "type": "Expression"
                            },
                            "type": "String"
                        },
                        "PipelineStart": {
                            "value": {
                                "value": "@variables('PipelineStart')",
                                "type": "Expression"
                            },
                            "type": "DateTime"
                        },
                        "RowsCopied": {
                            "value": {
                                "value": "0",
                                "type": "Expression"
                            },
                            "type": "Int32"
                        },
                        "RowsRead": {
                            "value": {
                                "value": "0",
                                "type": "Expression"
                            },
                            "type": "Int32"
                        },
                        "RowVersion": {
                            "value": {
                                "value": "0",
                                "type": "Expression"
                            },
                            "type": "Int64"
                        },
                        "Table": {
                            "value": {
                                "value": "@variables('Data_Source_Table_Name')",
                                "type": "Expression"
                            },
                            "type": "String"
                        },
                        "PipelineTriggerType": {
                            "value": {
                                "value": "@pipeline().TriggerType",
                                "type": "Expression"
                            },
                            "type": "String"
                        },
                        "ErrorMessage": {
                            "value": {
                                "value": "@activity('Execute Stored Procedure').error.message",
                                "type": "Expression"
                            },
                            "type": "string"
                        }
                    }
                },
                "linkedServiceName": {
                    "referenceName": "LSInsightDW",
                    "type": "LinkedServiceReference"
                }
            },
            {
                "name": "Last-RowID-Error",
                "description": "Stored procedure activity to store the new Pipeline run metadata in LSAudit table",
                "type": "SqlServerStoredProcedure",
                "dependsOn": [
                    {
                        "activity": "Last-RowID",
                        "dependencyConditions": [
                            "Failed"
                        ]
                    }
                ],
                "policy": {
                    "timeout": "7.00:00:00",
                    "retry": 0,
                    "retryIntervalInSeconds": 30
                },
                "userProperties": [],
                "typeProperties": {
                    "storedProcedureName": "[[dbo].[UpdateLSInsightAudit]",
                    "storedProcedureParameters": {
                        "ADFRunGUID": {
                            "value": {
                                "value": "@pipeline().RunId",
                                "type": "Expression"
                            },
                            "type": "String"
                        },
                        "CopyDuration": {
                            "value": {
                                "value": "0",
                                "type": "Expression"
                            },
                            "type": "Int32"
                        },
                        "PipelineName": {
                            "value": {
                                "value": "@concat('Failed Run - ',  pipeline().Pipeline)",
                                "type": "Expression"
                            },
                            "type": "String"
                        },
                        "PipelineStart": {
                            "value": {
                                "value": "@variables('PipelineStart')",
                                "type": "Expression"
                            },
                            "type": "DateTime"
                        },
                        "RowsCopied": {
                            "value": {
                                "value": "0",
                                "type": "Expression"
                            },
                            "type": "Int32"
                        },
                        "RowsRead": {
                            "value": {
                                "value": "0",
                                "type": "Expression"
                            },
                            "type": "Int32"
                        },
                        "RowVersion": {
                            "value": {
                                "value": "0",
                                "type": "Expression"
                            },
                            "type": "Int64"
                        },
                        "Table": {
                            "value": {
                                "value": "@variables('DestinationTable')",
                                "type": "Expression"
                            },
                            "type": "String"
                        },
                        "PipelineTriggerType": {
                            "value": {
                                "value": "@pipeline().TriggerType",
                                "type": "Expression"
                            },
                            "type": "String"
                        },
                        "ErrorMessage": {
                            "value": {
                                "value": "@activity('Last-RowID').output.Error.Message",
                                "type": "Expression"
                            },
                            "type": "string"
                        },
                        "Note": {
                            "value": ""
                        }
                    }
                },
                "linkedServiceName": {
                    "referenceName": "LSInsightDW",
                    "type": "LinkedServiceReference"
                }
            },
            {
                "name": "Execute Stored Procedure",
                "type": "Lookup",
                "dependsOn": [
                    {
                        "activity": "Last-RowID",
                        "dependencyConditions": [
                            "Succeeded"
                        ]
                    },
                    {
                        "activity": "Current-RowID",
                        "dependencyConditions": [
                            "Succeeded"
                        ]
                    }
                ],
                "policy": {
                    "timeout": "7.00:00:00",
                    "retry": 0,
                    "retryIntervalInSeconds": 30,
                    "secureOutput": false,
                    "secureInput": false
                },
                "userProperties": [],
                "typeProperties": {
                    "source": {
                        "type": "AzureSqlSource",
                        "sqlReaderStoredProcedureName": "[[dbo].[factPurchaseOrdersExec]",
                        "storedProcedureParameters": {
                            "NewRowID": {
                                "type": "Int64",
                                "value": {
                                    "value": "@activity('Current-RowID').output.firstRow.NewRowID",
                                    "type": "Expression"
                                }
                            },
                            "CurrentRowID": {
                                "type": "Int64",
                                "value": {
                                    "value": "@activity('Last-RowID').output.firstRow.LastRowID",
                                    "type": "Expression"
                                }
                            }
                        },
                        "queryTimeout": "23:00:00",
                        "partitionOption": "None"
                    },
                    "dataset": {
                        "referenceName": "LSInsightDW",
                        "type": "DatasetReference",
                        "parameters": {
                            "DestTableName": "NA"
                        }
                    }
                }
            },
            {
                "name": "Current-RowID",
                "description": "Retrieve  the current maximum value in watermark column of source data store",
                "type": "Lookup",
                "dependsOn": [],
                "policy": {
                    "timeout": "7.00:00:00",
                    "retry": 0,
                    "retryIntervalInSeconds": 30
                },
                "userProperties": [],
                "typeProperties": {
                    "source": {
                        "type": "AzureSqlSource",
                        "sqlReaderQuery": {
                            "value": "@concat('IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(''',variables('Data_Source_Table_Name'),'''))  select MAX(convert(bigint, ',pipeline().parameters.Data_Source_WaterMarkColumn, ')) as NewRowID from ',variables('Data_Source_Table_Name'), ' ELSE Select 0 AS NewRowID ')",
                            "type": "Expression"
                        },
                        "partitionOption": "None"
                    },
                    "dataset": {
                        "referenceName": "LSInsightDW",
                        "type": "DatasetReference",
                        "parameters": {
                            "DestTableName": "NA"
                        }
                    }
                }
            },
            {
                "name": "Current-RowID-Error",
                "description": "Stored procedure activity to store the new Pipeline run metadata in LSAudit table",
                "type": "SqlServerStoredProcedure",
                "dependsOn": [
                    {
                        "activity": "Current-RowID",
                        "dependencyConditions": [
                            "Failed"
                        ]
                    }
                ],
                "policy": {
                    "timeout": "7.00:00:00",
                    "retry": 0,
                    "retryIntervalInSeconds": 30
                },
                "userProperties": [],
                "typeProperties": {
                    "storedProcedureName": "[[dbo].[UpdateLSInsightAudit]",
                    "storedProcedureParameters": {
                        "ADFRunGUID": {
                            "value": {
                                "value": "@pipeline().RunId",
                                "type": "Expression"
                            },
                            "type": "String"
                        },
                        "CopyDuration": {
                            "value": {
                                "value": "0",
                                "type": "Expression"
                            },
                            "type": "Int32"
                        },
                        "PipelineName": {
                            "value": {
                                "value": "@concat('Failed Run - ',  pipeline().Pipeline)",
                                "type": "Expression"
                            },
                            "type": "String"
                        },
                        "PipelineStart": {
                            "value": {
                                "value": "@variables('PipelineStart')",
                                "type": "Expression"
                            },
                            "type": "DateTime"
                        },
                        "RowsCopied": {
                            "value": {
                                "value": "0",
                                "type": "Expression"
                            },
                            "type": "Int32"
                        },
                        "RowsRead": {
                            "value": {
                                "value": "0",
                                "type": "Expression"
                            },
                            "type": "Int32"
                        },
                        "RowVersion": {
                            "value": {
                                "value": "0",
                                "type": "Expression"
                            },
                            "type": "Int64"
                        },
                        "Table": {
                            "value": {
                                "value": "@variables('DestinationTable')",
                                "type": "Expression"
                            },
                            "type": "String"
                        },
                        "PipelineTriggerType": {
                            "value": {
                                "value": "@pipeline().TriggerType",
                                "type": "Expression"
                            },
                            "type": "String"
                        },
                        "ErrorMessage": {
                            "value": {
                                "value": "@activity('Current-RowID').output.Error.Message",
                                "type": "Expression"
                            },
                            "type": "string"
                        },
                        "Note": {
                            "value": ""
                        }
                    }
                },
                "linkedServiceName": {
                    "referenceName": "LSInsightDW",
                    "type": "LinkedServiceReference"
                }
            }
        ],
                "parameters": {
                    "Data_Source_WaterMarkColumn": {
                        "type": "String",
                        "defaultValue": "[[bigint_timestamp]"
                    },
                    "Control_Table_Table_Name": {
                        "type": "String",
                        "defaultValue": "[[dbo].[LSInsightAudit]"
                    },
                    "Control_Table_Column_Name": {
                        "type": "String",
                        "defaultValue": "[[NewRowVersion]"
                    },
                    "TablePrefix": {
                        "type": "string",
                        "defaultValue": "[parameters('TablePrefix')]"
                    }
                },
                "variables": {
                    "PipelineStart": {
                        "type": "String"
                    },
                    "DestinationTable": {
                        "type": "String",
                        "defaultValue": "[[DW].[fPurchaseOrders]"
                    },
                    "Data_Source_Table_Name": {
                        "type": "String",
                        "defaultValue": "[[stg$Purchase Line]"
                    },
                    "ErrorMessage": {
                        "type": "String"
                    }
                },
                "folder": {
                    "name": "4 - Fact Tables"
                },
                "annotations": []
           },
            "dependsOn": [
                "[concat(variables('factoryId'), '/linkedServices/LSInsightDW')]",
                "[concat(variables('factoryId'), '/datasets/LSInsightDW')]"  
            ]
        },
		
		{
            "name": "[concat(parameters('factoryName'), '/PL-SP-factPurchaseCreditMemo')]",
            "type": "Microsoft.DataFactory/factories/pipelines",
            "apiVersion": "2018-06-01",
            "properties": {
                "description": "Execute Stored Procedure in LS Insight DW",
                "activities": [
            {
                "name": "Last-RowID",
                "description": "Retrieve  the max rowversion value stored in the control table",
                "type": "Lookup",
                "dependsOn": [
                    {
                        "activity": "PipelineStart",
                        "dependencyConditions": [
                            "Succeeded"
                        ]
                    }
                ],
                "policy": {
                    "timeout": "7.00:00:00",
                    "retry": 0,
                    "retryIntervalInSeconds": 30
                },
                "userProperties": [],
                "typeProperties": {
                    "source": {
                        "type": "AzureSqlSource",
                        "sqlReaderQuery": {
                            "value": "@concat('SELECT COALESCE(max([NewRowVersion]),0)  AS LastRowID \n  FROM ', pipeline().parameters.Control_Table_Table_Name, '\n  where [Table] =''',variables('DestinationTable'),'''')",
                            "type": "Expression"
                        },
                        "partitionOption": "None"
                    },
                    "dataset": {
                        "referenceName": "LSInsightDW",
                        "type": "DatasetReference",
                        "parameters": {
                            "DestTableName": "NA"
                        }
                    },
                    "firstRowOnly": true
                }
            },
            {
                "name": "UpdateLSInsightAudit",
                "description": "Stored procedure activity to store the new Pipeline run metadata in LSAudit table",
                "type": "SqlServerStoredProcedure",
                "dependsOn": [
                    {
                        "activity": "Execute Stored Procedure",
                        "dependencyConditions": [
                            "Succeeded"
                        ]
                    }
                ],
                "policy": {
                    "timeout": "7.00:00:00",
                    "retry": 0,
                    "retryIntervalInSeconds": 30
                },
                "userProperties": [],
                "typeProperties": {
                    "storedProcedureName": "[[dbo].[UpdateLSInsightAudit]",
                    "storedProcedureParameters": {
                        "ADFRunGUID": {
                            "value": {
                                "value": "@pipeline().RunId",
                                "type": "Expression"
                            },
                            "type": "String"
                        },
                        "CopyDuration": {
                            "value": null,
                            "type": "Int32"
                        },
                        "PipelineName": {
                            "value": {
                                "value": "@pipeline().Pipeline",
                                "type": "Expression"
                            },
                            "type": "String"
                        },
                        "PipelineStart": {
                            "value": {
                                "value": "@variables('PipelineStart')",
                                "type": "Expression"
                            },
                            "type": "DateTime"
                        },
                        "RowsCopied": {
                            "value": {
                                "value": "@activity('Execute Stored Procedure').output.firstRow.RowCount",
                                "type": "Expression"
                            },
                            "type": "Int32"
                        },
                        "RowsRead": {
                            "value": null,
                            "type": "Int32"
                        },
                        "RowVersion": {
                            "value": {
                                "value": "@activity('Current-RowID').output.firstRow.NewRowID",
                                "type": "Expression"
                            },
                            "type": "Int64"
                        },
                        "Table": {
                            "value": {
                                "value": "@variables('DestinationTable')",
                                "type": "Expression"
                            },
                            "type": "String"
                        },
                        "PipelineTriggerType": {
                            "value": {
                                "value": "@pipeline().TriggerType",
                                "type": "Expression"
                            },
                            "type": "String"
                        },
                        "ErrorMessage": {
                            "value": null,
                            "type": "String"
                        },
                        "Note": {
                            "value": null
                        }
                    }
                },
                "linkedServiceName": {
                    "referenceName": "LSInsightDW",
                    "type": "LinkedServiceReference"
                }
            },
            {
                "name": "PipelineStart",
                "type": "SetVariable",
                "dependsOn": [],
                "userProperties": [],
                "typeProperties": {
                    "variableName": "PipelineStart",
                    "value": {
                        "value": "@utcnow()",
                        "type": "Expression"
                    }
                }
            },
            {
                "name": "On Error Update Audit",
                "description": "Stored procedure activity to store the new Pipeline run metadata in LSAudit table",
                "type": "SqlServerStoredProcedure",
                "dependsOn": [
                    {
                        "activity": "Execute Stored Procedure",
                        "dependencyConditions": [
                            "Failed"
                        ]
                    }
                ],
                "policy": {
                    "timeout": "7.00:00:00",
                    "retry": 0,
                    "retryIntervalInSeconds": 30
                },
                "userProperties": [],
                "typeProperties": {
                    "storedProcedureName": "[[dbo].[UpdateLSInsightAudit]",
                    "storedProcedureParameters": {
                        "ADFRunGUID": {
                            "value": {
                                "value": "@pipeline().RunId",
                                "type": "Expression"
                            },
                            "type": "String"
                        },
                        "CopyDuration": {
                            "value": {
                                "value": "0",
                                "type": "Expression"
                            },
                            "type": "Int32"
                        },
                        "PipelineName": {
                            "value": {
                                "value": "@concat('Failed Run - ',  pipeline().Pipeline)",
                                "type": "Expression"
                            },
                            "type": "String"
                        },
                        "PipelineStart": {
                            "value": {
                                "value": "@variables('PipelineStart')",
                                "type": "Expression"
                            },
                            "type": "DateTime"
                        },
                        "RowsCopied": {
                            "value": {
                                "value": "0",
                                "type": "Expression"
                            },
                            "type": "Int32"
                        },
                        "RowsRead": {
                            "value": {
                                "value": "0",
                                "type": "Expression"
                            },
                            "type": "Int32"
                        },
                        "RowVersion": {
                            "value": {
                                "value": "0",
                                "type": "Expression"
                            },
                            "type": "Int64"
                        },
                        "Table": {
                            "value": {
                                "value": "@variables('Data_Source_Table_Name')",
                                "type": "Expression"
                            },
                            "type": "String"
                        },
                        "PipelineTriggerType": {
                            "value": {
                                "value": "@pipeline().TriggerType",
                                "type": "Expression"
                            },
                            "type": "String"
                        },
                        "ErrorMessage": {
                            "value": {
                                "value": "@activity('Execute Stored Procedure').error.message",
                                "type": "Expression"
                            },
                            "type": "string"
                        }
                    }
                },
                "linkedServiceName": {
                    "referenceName": "LSInsightDW",
                    "type": "LinkedServiceReference"
                }
            },
            {
                "name": "Last-RowID-Error",
                "description": "Stored procedure activity to store the new Pipeline run metadata in LSAudit table",
                "type": "SqlServerStoredProcedure",
                "dependsOn": [
                    {
                        "activity": "Last-RowID",
                        "dependencyConditions": [
                            "Failed"
                        ]
                    }
                ],
                "policy": {
                    "timeout": "7.00:00:00",
                    "retry": 0,
                    "retryIntervalInSeconds": 30
                },
                "userProperties": [],
                "typeProperties": {
                    "storedProcedureName": "[[dbo].[UpdateLSInsightAudit]",
                    "storedProcedureParameters": {
                        "ADFRunGUID": {
                            "value": {
                                "value": "@pipeline().RunId",
                                "type": "Expression"
                            },
                            "type": "String"
                        },
                        "CopyDuration": {
                            "value": {
                                "value": "0",
                                "type": "Expression"
                            },
                            "type": "Int32"
                        },
                        "PipelineName": {
                            "value": {
                                "value": "@concat('Failed Run - ',  pipeline().Pipeline)",
                                "type": "Expression"
                            },
                            "type": "String"
                        },
                        "PipelineStart": {
                            "value": {
                                "value": "@variables('PipelineStart')",
                                "type": "Expression"
                            },
                            "type": "DateTime"
                        },
                        "RowsCopied": {
                            "value": {
                                "value": "0",
                                "type": "Expression"
                            },
                            "type": "Int32"
                        },
                        "RowsRead": {
                            "value": {
                                "value": "0",
                                "type": "Expression"
                            },
                            "type": "Int32"
                        },
                        "RowVersion": {
                            "value": {
                                "value": "0",
                                "type": "Expression"
                            },
                            "type": "Int64"
                        },
                        "Table": {
                            "value": {
                                "value": "@variables('DestinationTable')",
                                "type": "Expression"
                            },
                            "type": "String"
                        },
                        "PipelineTriggerType": {
                            "value": {
                                "value": "@pipeline().TriggerType",
                                "type": "Expression"
                            },
                            "type": "String"
                        },
                        "ErrorMessage": {
                            "value": {
                                "value": "@activity('Last-RowID').output.Error.Message",
                                "type": "Expression"
                            },
                            "type": "string"
                        },
                        "Note": {
                            "value": ""
                        }
                    }
                },
                "linkedServiceName": {
                    "referenceName": "LSInsightDW",
                    "type": "LinkedServiceReference"
                }
            },
            {
                "name": "Execute Stored Procedure",
                "type": "Lookup",
                "dependsOn": [
                    {
                        "activity": "Last-RowID",
                        "dependencyConditions": [
                            "Succeeded"
                        ]
                    },
                    {
                        "activity": "Current-RowID",
                        "dependencyConditions": [
                            "Succeeded"
                        ]
                    }
                ],
                "policy": {
                    "timeout": "7.00:00:00",
                    "retry": 0,
                    "retryIntervalInSeconds": 30,
                    "secureOutput": false,
                    "secureInput": false
                },
                "userProperties": [],
                "typeProperties": {
                    "source": {
                        "type": "AzureSqlSource",
                        "sqlReaderStoredProcedureName": "[[dbo].[factPurchaseCreditMemoExec]",
                        "storedProcedureParameters": {
                            "NewRowID": {
                                "type": "Int64",
                                "value": {
                                    "value": "@activity('Current-RowID').output.firstRow.NewRowID",
                                    "type": "Expression"
                                }
                            },
                            "CurrentRowID": {
                                "type": "Int64",
                                "value": {
                                    "value": "@activity('Last-RowID').output.firstRow.LastRowID",
                                    "type": "Expression"
                                }
                            }
                        },
                        "queryTimeout": "23:00:00",
                        "partitionOption": "None"
                    },
                    "dataset": {
                        "referenceName": "LSInsightDW",
                        "type": "DatasetReference",
                        "parameters": {
                            "DestTableName": "NA"
                        }
                    }
                }
            },
            {
                "name": "Current-RowID",
                "description": "Retrieve  the current maximum value in watermark column of source data store",
                "type": "Lookup",
                "dependsOn": [],
                "policy": {
                    "timeout": "7.00:00:00",
                    "retry": 0,
                    "retryIntervalInSeconds": 30
                },
                "userProperties": [],
                "typeProperties": {
                    "source": {
                        "type": "AzureSqlSource",
                        "sqlReaderQuery": {
                            "value": "@concat('IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(''',variables('Data_Source_Table_Name'),'''))  select MAX(convert(bigint, ',pipeline().parameters.Data_Source_WaterMarkColumn, ')) as NewRowID from ',variables('Data_Source_Table_Name'), ' ELSE Select 0 AS NewRowID ')",
                            "type": "Expression"
                        },
                        "partitionOption": "None"
                    },
                    "dataset": {
                        "referenceName": "LSInsightDW",
                        "type": "DatasetReference",
                        "parameters": {
                            "DestTableName": "NA"
                        }
                    }
                }
            },
            {
                "name": "Current-RowID-Error",
                "description": "Stored procedure activity to store the new Pipeline run metadata in LSAudit table",
                "type": "SqlServerStoredProcedure",
                "dependsOn": [
                    {
                        "activity": "Current-RowID",
                        "dependencyConditions": [
                            "Failed"
                        ]
                    }
                ],
                "policy": {
                    "timeout": "7.00:00:00",
                    "retry": 0,
                    "retryIntervalInSeconds": 30
                },
                "userProperties": [],
                "typeProperties": {
                    "storedProcedureName": "[[dbo].[UpdateLSInsightAudit]",
                    "storedProcedureParameters": {
                        "ADFRunGUID": {
                            "value": {
                                "value": "@pipeline().RunId",
                                "type": "Expression"
                            },
                            "type": "String"
                        },
                        "CopyDuration": {
                            "value": {
                                "value": "0",
                                "type": "Expression"
                            },
                            "type": "Int32"
                        },
                        "PipelineName": {
                            "value": {
                                "value": "@concat('Failed Run - ',  pipeline().Pipeline)",
                                "type": "Expression"
                            },
                            "type": "String"
                        },
                        "PipelineStart": {
                            "value": {
                                "value": "@variables('PipelineStart')",
                                "type": "Expression"
                            },
                            "type": "DateTime"
                        },
                        "RowsCopied": {
                            "value": {
                                "value": "0",
                                "type": "Expression"
                            },
                            "type": "Int32"
                        },
                        "RowsRead": {
                            "value": {
                                "value": "0",
                                "type": "Expression"
                            },
                            "type": "Int32"
                        },
                        "RowVersion": {
                            "value": {
                                "value": "0",
                                "type": "Expression"
                            },
                            "type": "Int64"
                        },
                        "Table": {
                            "value": {
                                "value": "@variables('DestinationTable')",
                                "type": "Expression"
                            },
                            "type": "String"
                        },
                        "PipelineTriggerType": {
                            "value": {
                                "value": "@pipeline().TriggerType",
                                "type": "Expression"
                            },
                            "type": "String"
                        },
                        "ErrorMessage": {
                            "value": {
                                "value": "@activity('Current-RowID').output.Error.Message",
                                "type": "Expression"
                            },
                            "type": "string"
                        },
                        "Note": {
                            "value": ""
                        }
                    }
                },
                "linkedServiceName": {
                    "referenceName": "LSInsightDW",
                    "type": "LinkedServiceReference"
                }
            }
        ],
                "parameters": {
                    "Data_Source_WaterMarkColumn": {
                        "type": "String",
                        "defaultValue": "[[bigint_timestamp]"
                    },
                    "Control_Table_Table_Name": {
                        "type": "String",
                        "defaultValue": "[[dbo].[LSInsightAudit]"
                    },
                    "Control_Table_Column_Name": {
                        "type": "String",
                        "defaultValue": "[[NewRowVersion]"
                    },
                    "TablePrefix": {
                        "type": "string",
                        "defaultValue": "[parameters('TablePrefix')]"
                    }
                },
                "variables": {
                    "PipelineStart": {
                        "type": "String"
                    },
                    "DestinationTable": {
                        "type": "String",
                        "defaultValue": "[[DW].[fPurchaseCreditMemo]"
                    },
                    "Data_Source_Table_Name": {
                        "type": "String",
                        "defaultValue": "[[Stg$Purch_ Cr_ Memo Line]"
                    },
                    "ErrorMessage": {
                        "type": "String"
                    }
                },
                "folder": {
                    "name": "4 - Fact Tables"
                },
                "annotations": []
           },
            "dependsOn": [
                "[concat(variables('factoryId'), '/linkedServices/LSInsightDW')]",
                "[concat(variables('factoryId'), '/datasets/LSInsightDW')]"  
            ]
        },
		
		{
            "name": "[concat(parameters('factoryName'), '/PL-SP-factPurchaseInvoice')]",
            "type": "Microsoft.DataFactory/factories/pipelines",
            "apiVersion": "2018-06-01",
            "properties": {
                "description": "Execute Stored Procedure in LS Insight DW",
                "activities": [
            {
                "name": "Last-RowID",
                "description": "Retrieve  the max rowversion value stored in the control table",
                "type": "Lookup",
                "dependsOn": [
                    {
                        "activity": "PipelineStart",
                        "dependencyConditions": [
                            "Succeeded"
                        ]
                    }
                ],
                "policy": {
                    "timeout": "7.00:00:00",
                    "retry": 0,
                    "retryIntervalInSeconds": 30
                },
                "userProperties": [],
                "typeProperties": {
                    "source": {
                        "type": "AzureSqlSource",
                        "sqlReaderQuery": {
                            "value": "@concat('SELECT COALESCE(max([NewRowVersion]),0)  AS LastRowID \n  FROM ', pipeline().parameters.Control_Table_Table_Name, '\n  where [Table] =''',variables('DestinationTable'),'''')",
                            "type": "Expression"
                        },
                        "partitionOption": "None"
                    },
                    "dataset": {
                        "referenceName": "LSInsightDW",
                        "type": "DatasetReference",
                        "parameters": {
                            "DestTableName": "NA"
                        }
                    },
                    "firstRowOnly": true
                }
            },
            {
                "name": "UpdateLSInsightAudit",
                "description": "Stored procedure activity to store the new Pipeline run metadata in LSAudit table",
                "type": "SqlServerStoredProcedure",
                "dependsOn": [
                    {
                        "activity": "Execute Stored Procedure",
                        "dependencyConditions": [
                            "Succeeded"
                        ]
                    }
                ],
                "policy": {
                    "timeout": "7.00:00:00",
                    "retry": 0,
                    "retryIntervalInSeconds": 30
                },
                "userProperties": [],
                "typeProperties": {
                    "storedProcedureName": "[[dbo].[UpdateLSInsightAudit]",
                    "storedProcedureParameters": {
                        "ADFRunGUID": {
                            "value": {
                                "value": "@pipeline().RunId",
                                "type": "Expression"
                            },
                            "type": "String"
                        },
                        "CopyDuration": {
                            "value": null,
                            "type": "Int32"
                        },
                        "PipelineName": {
                            "value": {
                                "value": "@pipeline().Pipeline",
                                "type": "Expression"
                            },
                            "type": "String"
                        },
                        "PipelineStart": {
                            "value": {
                                "value": "@variables('PipelineStart')",
                                "type": "Expression"
                            },
                            "type": "DateTime"
                        },
                        "RowsCopied": {
                            "value": {
                                "value": "@activity('Execute Stored Procedure').output.firstRow.RowCount",
                                "type": "Expression"
                            },
                            "type": "Int32"
                        },
                        "RowsRead": {
                            "value": null,
                            "type": "Int32"
                        },
                        "RowVersion": {
                            "value": {
                                "value": "@activity('Current-RowID').output.firstRow.NewRowID",
                                "type": "Expression"
                            },
                            "type": "Int64"
                        },
                        "Table": {
                            "value": {
                                "value": "@variables('DestinationTable')",
                                "type": "Expression"
                            },
                            "type": "String"
                        },
                        "PipelineTriggerType": {
                            "value": {
                                "value": "@pipeline().TriggerType",
                                "type": "Expression"
                            },
                            "type": "String"
                        },
                        "ErrorMessage": {
                            "value": null,
                            "type": "String"
                        },
                        "Note": {
                            "value": null
                        }
                    }
                },
                "linkedServiceName": {
                    "referenceName": "LSInsightDW",
                    "type": "LinkedServiceReference"
                }
            },
            {
                "name": "PipelineStart",
                "type": "SetVariable",
                "dependsOn": [],
                "userProperties": [],
                "typeProperties": {
                    "variableName": "PipelineStart",
                    "value": {
                        "value": "@utcnow()",
                        "type": "Expression"
                    }
                }
            },
            {
                "name": "On Error Update Audit",
                "description": "Stored procedure activity to store the new Pipeline run metadata in LSAudit table",
                "type": "SqlServerStoredProcedure",
                "dependsOn": [
                    {
                        "activity": "Execute Stored Procedure",
                        "dependencyConditions": [
                            "Failed"
                        ]
                    }
                ],
                "policy": {
                    "timeout": "7.00:00:00",
                    "retry": 0,
                    "retryIntervalInSeconds": 30
                },
                "userProperties": [],
                "typeProperties": {
                    "storedProcedureName": "[[dbo].[UpdateLSInsightAudit]",
                    "storedProcedureParameters": {
                        "ADFRunGUID": {
                            "value": {
                                "value": "@pipeline().RunId",
                                "type": "Expression"
                            },
                            "type": "String"
                        },
                        "CopyDuration": {
                            "value": {
                                "value": "0",
                                "type": "Expression"
                            },
                            "type": "Int32"
                        },
                        "PipelineName": {
                            "value": {
                                "value": "@concat('Failed Run - ',  pipeline().Pipeline)",
                                "type": "Expression"
                            },
                            "type": "String"
                        },
                        "PipelineStart": {
                            "value": {
                                "value": "@variables('PipelineStart')",
                                "type": "Expression"
                            },
                            "type": "DateTime"
                        },
                        "RowsCopied": {
                            "value": {
                                "value": "0",
                                "type": "Expression"
                            },
                            "type": "Int32"
                        },
                        "RowsRead": {
                            "value": {
                                "value": "0",
                                "type": "Expression"
                            },
                            "type": "Int32"
                        },
                        "RowVersion": {
                            "value": {
                                "value": "0",
                                "type": "Expression"
                            },
                            "type": "Int64"
                        },
                        "Table": {
                            "value": {
                                "value": "@variables('Data_Source_Table_Name')",
                                "type": "Expression"
                            },
                            "type": "String"
                        },
                        "PipelineTriggerType": {
                            "value": {
                                "value": "@pipeline().TriggerType",
                                "type": "Expression"
                            },
                            "type": "String"
                        },
                        "ErrorMessage": {
                            "value": {
                                "value": "@activity('Execute Stored Procedure').error.message",
                                "type": "Expression"
                            },
                            "type": "string"
                        }
                    }
                },
                "linkedServiceName": {
                    "referenceName": "LSInsightDW",
                    "type": "LinkedServiceReference"
                }
            },
            {
                "name": "Last-RowID-Error",
                "description": "Stored procedure activity to store the new Pipeline run metadata in LSAudit table",
                "type": "SqlServerStoredProcedure",
                "dependsOn": [
                    {
                        "activity": "Last-RowID",
                        "dependencyConditions": [
                            "Failed"
                        ]
                    }
                ],
                "policy": {
                    "timeout": "7.00:00:00",
                    "retry": 0,
                    "retryIntervalInSeconds": 30
                },
                "userProperties": [],
                "typeProperties": {
                    "storedProcedureName": "[[dbo].[UpdateLSInsightAudit]",
                    "storedProcedureParameters": {
                        "ADFRunGUID": {
                            "value": {
                                "value": "@pipeline().RunId",
                                "type": "Expression"
                            },
                            "type": "String"
                        },
                        "CopyDuration": {
                            "value": {
                                "value": "0",
                                "type": "Expression"
                            },
                            "type": "Int32"
                        },
                        "PipelineName": {
                            "value": {
                                "value": "@concat('Failed Run - ',  pipeline().Pipeline)",
                                "type": "Expression"
                            },
                            "type": "String"
                        },
                        "PipelineStart": {
                            "value": {
                                "value": "@variables('PipelineStart')",
                                "type": "Expression"
                            },
                            "type": "DateTime"
                        },
                        "RowsCopied": {
                            "value": {
                                "value": "0",
                                "type": "Expression"
                            },
                            "type": "Int32"
                        },
                        "RowsRead": {
                            "value": {
                                "value": "0",
                                "type": "Expression"
                            },
                            "type": "Int32"
                        },
                        "RowVersion": {
                            "value": {
                                "value": "0",
                                "type": "Expression"
                            },
                            "type": "Int64"
                        },
                        "Table": {
                            "value": {
                                "value": "@variables('DestinationTable')",
                                "type": "Expression"
                            },
                            "type": "String"
                        },
                        "PipelineTriggerType": {
                            "value": {
                                "value": "@pipeline().TriggerType",
                                "type": "Expression"
                            },
                            "type": "String"
                        },
                        "ErrorMessage": {
                            "value": {
                                "value": "@activity('Last-RowID').output.Error.Message",
                                "type": "Expression"
                            },
                            "type": "string"
                        },
                        "Note": {
                            "value": ""
                        }
                    }
                },
                "linkedServiceName": {
                    "referenceName": "LSInsightDW",
                    "type": "LinkedServiceReference"
                }
            },
            {
                "name": "Execute Stored Procedure",
                "type": "Lookup",
                "dependsOn": [
                    {
                        "activity": "Last-RowID",
                        "dependencyConditions": [
                            "Succeeded"
                        ]
                    },
                    {
                        "activity": "Current-RowID",
                        "dependencyConditions": [
                            "Succeeded"
                        ]
                    }
                ],
                "policy": {
                    "timeout": "7.00:00:00",
                    "retry": 0,
                    "retryIntervalInSeconds": 30,
                    "secureOutput": false,
                    "secureInput": false
                },
                "userProperties": [],
                "typeProperties": {
                    "source": {
                        "type": "AzureSqlSource",
                        "sqlReaderStoredProcedureName": "[[dbo].[factPurchaseInvoiceExec]",
                        "storedProcedureParameters": {
                            "NewRowID": {
                                "type": "Int64",
                                "value": {
                                    "value": "@activity('Current-RowID').output.firstRow.NewRowID",
                                    "type": "Expression"
                                }
                            },
                            "CurrentRowID": {
                                "type": "Int64",
                                "value": {
                                    "value": "@activity('Last-RowID').output.firstRow.LastRowID",
                                    "type": "Expression"
                                }
                            }
                        },
                        "queryTimeout": "23:00:00",
                        "partitionOption": "None"
                    },
                    "dataset": {
                        "referenceName": "LSInsightDW",
                        "type": "DatasetReference",
                        "parameters": {
                            "DestTableName": "NA"
                        }
                    }
                }
            },
            {
                "name": "Current-RowID",
                "description": "Retrieve  the current maximum value in watermark column of source data store",
                "type": "Lookup",
                "dependsOn": [],
                "policy": {
                    "timeout": "7.00:00:00",
                    "retry": 0,
                    "retryIntervalInSeconds": 30
                },
                "userProperties": [],
                "typeProperties": {
                    "source": {
                        "type": "AzureSqlSource",
                        "sqlReaderQuery": {
                            "value": "@concat('IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(''',variables('Data_Source_Table_Name'),'''))  select MAX(convert(bigint, ',pipeline().parameters.Data_Source_WaterMarkColumn, ')) as NewRowID from ',variables('Data_Source_Table_Name'), ' ELSE Select 0 AS NewRowID ')",
                            "type": "Expression"
                        },
                        "partitionOption": "None"
                    },
                    "dataset": {
                        "referenceName": "LSInsightDW",
                        "type": "DatasetReference",
                        "parameters": {
                            "DestTableName": "NA"
                        }
                    }
                }
            },
            {
                "name": "Current-RowID-Error",
                "description": "Stored procedure activity to store the new Pipeline run metadata in LSAudit table",
                "type": "SqlServerStoredProcedure",
                "dependsOn": [
                    {
                        "activity": "Current-RowID",
                        "dependencyConditions": [
                            "Failed"
                        ]
                    }
                ],
                "policy": {
                    "timeout": "7.00:00:00",
                    "retry": 0,
                    "retryIntervalInSeconds": 30
                },
                "userProperties": [],
                "typeProperties": {
                    "storedProcedureName": "[[dbo].[UpdateLSInsightAudit]",
                    "storedProcedureParameters": {
                        "ADFRunGUID": {
                            "value": {
                                "value": "@pipeline().RunId",
                                "type": "Expression"
                            },
                            "type": "String"
                        },
                        "CopyDuration": {
                            "value": {
                                "value": "0",
                                "type": "Expression"
                            },
                            "type": "Int32"
                        },
                        "PipelineName": {
                            "value": {
                                "value": "@concat('Failed Run - ',  pipeline().Pipeline)",
                                "type": "Expression"
                            },
                            "type": "String"
                        },
                        "PipelineStart": {
                            "value": {
                                "value": "@variables('PipelineStart')",
                                "type": "Expression"
                            },
                            "type": "DateTime"
                        },
                        "RowsCopied": {
                            "value": {
                                "value": "0",
                                "type": "Expression"
                            },
                            "type": "Int32"
                        },
                        "RowsRead": {
                            "value": {
                                "value": "0",
                                "type": "Expression"
                            },
                            "type": "Int32"
                        },
                        "RowVersion": {
                            "value": {
                                "value": "0",
                                "type": "Expression"
                            },
                            "type": "Int64"
                        },
                        "Table": {
                            "value": {
                                "value": "@variables('DestinationTable')",
                                "type": "Expression"
                            },
                            "type": "String"
                        },
                        "PipelineTriggerType": {
                            "value": {
                                "value": "@pipeline().TriggerType",
                                "type": "Expression"
                            },
                            "type": "String"
                        },
                        "ErrorMessage": {
                            "value": {
                                "value": "@activity('Current-RowID').output.Error.Message",
                                "type": "Expression"
                            },
                            "type": "string"
                        },
                        "Note": {
                            "value": ""
                        }
                    }
                },
                "linkedServiceName": {
                    "referenceName": "LSInsightDW",
                    "type": "LinkedServiceReference"
                }
            }
        ],
                "parameters": {
                    "Data_Source_WaterMarkColumn": {
                        "type": "String",
                        "defaultValue": "[[bigint_timestamp]"
                    },
                    "Control_Table_Table_Name": {
                        "type": "String",
                        "defaultValue": "[[dbo].[LSInsightAudit]"
                    },
                    "Control_Table_Column_Name": {
                        "type": "String",
                        "defaultValue": "[[NewRowVersion]"
                    },
                    "TablePrefix": {
                        "type": "string",
                        "defaultValue": "[parameters('TablePrefix')]"
                    }
                },
                "variables": {
                    "PipelineStart": {
                        "type": "String"
                    },
                    "DestinationTable": {
                        "type": "String",
                        "defaultValue": "[[DW].[fPurchaseInvoice]"
                    },
                    "Data_Source_Table_Name": {
                        "type": "String",
                        "defaultValue": "[[stg$Purch_ Inv_ Line]"
                    },
                    "ErrorMessage": {
                        "type": "String"
                    }
                },
                "folder": {
                    "name": "4 - Fact Tables"
                },
                "annotations": []
           },
            "dependsOn": [
                "[concat(variables('factoryId'), '/linkedServices/LSInsightDW')]",
                "[concat(variables('factoryId'), '/datasets/LSInsightDW')]"  
            ]
        },
		

        {
            "name": "[concat(parameters('factoryName'), '/PL-SP-factInventory')]",
            "type": "Microsoft.DataFactory/factories/pipelines",
            "apiVersion": "2018-06-01",
            "properties": {
                "description": "Execute Stored Procedure in LS Insight DW",
                "activities": [
                    {
                        "name": "Last-RowID",
                        "description": "Retrieve  the max rowversion value stored in the control table",
                        "type": "Lookup",
                        "dependsOn": [
                            {
                                "activity": "PipelineStart",
                                "dependencyConditions": [
                                    "Succeeded"
                                ]
                            }
                        ],
                        "policy": {
                            "timeout": "7.00:00:00",
                            "retry": 0,
                            "retryIntervalInSeconds": 30
                        },
                        "userProperties": [],
                        "typeProperties": {
                            "source": {
                                "type": "AzureSqlSource",
                                "sqlReaderQuery": {
                                    "value": "@concat('SELECT COALESCE(max([NewRowVersion]),0)  AS LastRowID \n  FROM ', pipeline().parameters.Control_Table_Table_Name, '\n  where [Table] =''',variables('DestinationTable'),'''')",
                                    "type": "Expression"
                                },
                                "partitionOption": "None"
                            },
                            "dataset": {
                                "referenceName": "LSInsightDW",
                                "type": "DatasetReference",
                                "parameters": {
                                    "DestTableName": "NA"
                                }
                            },
                            "firstRowOnly": true
                        }
                    },
                    {
                        "name": "UpdateLSInsightAudit-TSE",
                        "description": "Stored procedure activity to store the new Pipeline run metadata in LSAudit table",
                        "type": "SqlServerStoredProcedure",
                        "dependsOn": [
                            {
                                "activity": "Execute Stored Procedure",
                                "dependencyConditions": [
                                    "Succeeded"
                                ]
                            }
                        ],
                        "policy": {
                            "timeout": "7.00:00:00",
                            "retry": 0,
                            "retryIntervalInSeconds": 30
                        },
                        "userProperties": [],
                        "typeProperties": {
                            "storedProcedureName": "[[dbo].[UpdateLSInsightAudit]",
                            "storedProcedureParameters": {
                                "ADFRunGUID": {
                                    "value": {
                                        "value": "@pipeline().RunId",
                                        "type": "Expression"
                                    },
                                    "type": "String"
                                },
                                "CopyDuration": {
                                    "value": null,
                                    "type": "Int32"
                                },
                                "PipelineName": {
                                    "value": {
                                        "value": "@pipeline().Pipeline",
                                        "type": "Expression"
                                    },
                                    "type": "String"
                                },
                                "PipelineStart": {
                                    "value": {
                                        "value": "@variables('PipelineStart')",
                                        "type": "Expression"
                                    },
                                    "type": "DateTime"
                                },
                                "RowsCopied": {
                                    "value": {
                                        "value": "@activity('Execute Stored Procedure').output.firstRow.RowCount",
                                        "type": "Expression"
                                    },
                                    "type": "Int32"
                                },
                                "RowsRead": {
                                    "value": null,
                                    "type": "Int32"
                                },
                                "RowVersion": {
                                    "value": {
                                        "value": "@activity('Current-RowID').output.firstRow.NewRowID",
                                        "type": "Expression"
                                    },
                                    "type": "Int64"
                                },
                                "Table": {
                                    "value": {
                                        "value": "@variables('DestinationTable')",
                                        "type": "Expression"
                                    },
                                    "type": "String"
                                },
                                "PipelineTriggerType": {
                                    "value": {
                                        "value": "@pipeline().TriggerType",
                                        "type": "Expression"
                                    },
                                    "type": "String"
                                },
                                "ErrorMessage": {
                                    "value": null,
                                    "type": "String"
                                },
                                "Note": {
                                    "value": null
                                }
                            }
                        },
                        "linkedServiceName": {
                            "referenceName": "LSInsightDW",
                            "type": "LinkedServiceReference"
                        }
                    },
                    {
                        "name": "PipelineStart",
                        "type": "SetVariable",
                        "dependsOn": [],
                        "userProperties": [],
                        "typeProperties": {
                            "variableName": "PipelineStart",
                            "value": {
                                "value": "@utcnow()",
                                "type": "Expression"
                            }
                        }
                    },
                    {
                        "name": "SqlServerStoredProcedure1",
                        "description": "Stored procedure activity to store the new Pipeline run metadata in LSAudit table",
                        "type": "SqlServerStoredProcedure",
                        "dependsOn": [
                            {
                                "activity": "Execute Stored Procedure",
                                "dependencyConditions": [
                                    "Failed"
                                ]
                            }
                        ],
                        "policy": {
                            "timeout": "7.00:00:00",
                            "retry": 0,
                            "retryIntervalInSeconds": 30
                        },
                        "userProperties": [],
                        "typeProperties": {
                            "storedProcedureName": "[[dbo].[UpdateLSInsightAudit]",
                            "storedProcedureParameters": {
                                "ADFRunGUID": {
                                    "value": {
                                        "value": "@pipeline().RunId",
                                        "type": "Expression"
                                    },
                                    "type": "String"
                                },
                                "CopyDuration": {
                                    "value": {
                                        "value": "0",
                                        "type": "Expression"
                                    },
                                    "type": "Int32"
                                },
                                "PipelineName": {
                                    "value": {
                                        "value": "@concat('Failed Run - ',  pipeline().Pipeline)",
                                        "type": "Expression"
                                    },
                                    "type": "String"
                                },
                                "PipelineStart": {
                                    "value": {
                                        "value": "@variables('PipelineStart')",
                                        "type": "Expression"
                                    },
                                    "type": "DateTime"
                                },
                                "RowsCopied": {
                                    "value": {
                                        "value": "0",
                                        "type": "Expression"
                                    },
                                    "type": "Int32"
                                },
                                "RowsRead": {
                                    "value": {
                                        "value": "0",
                                        "type": "Expression"
                                    },
                                    "type": "Int32"
                                },
                                "RowVersion": {
                                    "value": {
                                        "value": "0",
                                        "type": "Expression"
                                    },
                                    "type": "Int64"
                                },
                                "Table": {
                                    "value": {
                                        "value": "@variables('Data_Source_Table_Name')",
                                        "type": "Expression"
                                    },
                                    "type": "String"
                                },
                                "PipelineTriggerType": {
                                    "value": {
                                        "value": "@pipeline().TriggerType",
                                        "type": "Expression"
                                    },
                                    "type": "String"
                                },
                                "ErrorMessage": {
                                    "value": {
                                        "value": "@activity('Execute Stored Procedure').error.message",
                                        "type": "Expression"
                                    },
                                    "type": "string"
                                }
                            }
                        },
                        "linkedServiceName": {
                            "referenceName": "LSInsightDW",
                            "type": "LinkedServiceReference"
                        }
                    },
                    {
                        "name": "Last-TSE-RowID-Error",
                        "description": "Stored procedure activity to store the new Pipeline run metadata in LSAudit table",
                        "type": "SqlServerStoredProcedure",
                        "dependsOn": [
                            {
                                "activity": "Last-RowID",
                                "dependencyConditions": [
                                    "Failed"
                                ]
                            }
                        ],
                        "policy": {
                            "timeout": "7.00:00:00",
                            "retry": 0,
                            "retryIntervalInSeconds": 30
                        },
                        "userProperties": [],
                        "typeProperties": {
                            "storedProcedureName": "[[dbo].[UpdateLSInsightAudit]",
                            "storedProcedureParameters": {
                                "ADFRunGUID": {
                                    "value": {
                                        "value": "@pipeline().RunId",
                                        "type": "Expression"
                                    },
                                    "type": "String"
                                },
                                "CopyDuration": {
                                    "value": {
                                        "value": "0",
                                        "type": "Expression"
                                    },
                                    "type": "Int32"
                                },
                                "PipelineName": {
                                    "value": {
                                        "value": "@concat('Failed Run - ',  pipeline().Pipeline)",
                                        "type": "Expression"
                                    },
                                    "type": "String"
                                },
                                "PipelineStart": {
                                    "value": {
                                        "value": "@variables('PipelineStart')",
                                        "type": "Expression"
                                    },
                                    "type": "DateTime"
                                },
                                "RowsCopied": {
                                    "value": {
                                        "value": "0",
                                        "type": "Expression"
                                    },
                                    "type": "Int32"
                                },
                                "RowsRead": {
                                    "value": {
                                        "value": "0",
                                        "type": "Expression"
                                    },
                                    "type": "Int32"
                                },
                                "RowVersion": {
                                    "value": {
                                        "value": "0",
                                        "type": "Expression"
                                    },
                                    "type": "Int64"
                                },
                                "Table": {
                                    "value": {
                                        "value": "@variables('DestinationTable')",
                                        "type": "Expression"
                                    },
                                    "type": "String"
                                },
                                "PipelineTriggerType": {
                                    "value": {
                                        "value": "@pipeline().TriggerType",
                                        "type": "Expression"
                                    },
                                    "type": "String"
                                },
                                "ErrorMessage": {
                                    "value": {
                                        "value": "@activity('Last-RowID').output.Error.Message",
                                        "type": "Expression"
                                    },
                                    "type": "string"
                                },
                                "Note": {
                                    "value": "TSE"
                                }
                            }
                        },
                        "linkedServiceName": {
                            "referenceName": "LSInsightDW",
                            "type": "LinkedServiceReference"
                        }
                    },
                    {
                        "name": "Execute Stored Procedure",
                        "type": "Lookup",
                        "dependsOn": [
                            {
                                "activity": "Last-RowID",
                                "dependencyConditions": [
                                    "Succeeded"
                                ]
                            },
                            {
                                "activity": "Current-RowID",
                                "dependencyConditions": [
                                    "Succeeded"
                                ]
                            }
                        ],
                        "policy": {
                            "timeout": "7.00:00:00",
                            "retry": 0,
                            "retryIntervalInSeconds": 30,
                            "secureOutput": false,
                            "secureInput": false
                        },
                        "userProperties": [],
                        "typeProperties": {
                            "source": {
                                "type": "AzureSqlSource",
                                "sqlReaderStoredProcedureName": "[[dbo].[factInventory]",
                                "storedProcedureParameters": {
                                    "InventoryNewRowID": {
                                        "type": "Int64",
                                        "value": {
                                            "value": "@activity('Current-RowID').output.firstRow.NewRowID",
                                            "type": "Expression"
                                        }
                                    },
                                    "InventoryRowID": {
                                        "type": "Int64",
                                        "value": {
                                            "value": "@activity('Last-RowID').output.firstRow.LastRowID",
                                            "type": "Expression"
                                        }
                                    }
                                },
                                "queryTimeout": "23:00:00",
                                "partitionOption": "None"
                            },
                            "dataset": {
                                "referenceName": "LSInsightDW",
                                "type": "DatasetReference",
                                "parameters": {
                                    "DestTableName": "NA"
                                }
                            }
                        }
                    },
                    {
                        "name": "Current-RowID",
                        "description": "Retrieve  the current maximum value in watermark column of source data store",
                        "type": "Lookup",
                        "dependsOn": [],
                        "policy": {
                            "timeout": "7.00:00:00",
                            "retry": 0,
                            "retryIntervalInSeconds": 30
                        },
                        "userProperties": [],
                        "typeProperties": {
                            "source": {
                                "type": "AzureSqlSource",
                                "sqlReaderQuery": {
                                    "value": "@concat('IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(''',variables('Data_Source_Table_Name'),'''))  select MAX(convert(bigint, ',pipeline().parameters.Data_Source_WaterMarkColumn, ')) as NewRowID from ',variables('Data_Source_Table_Name'), ' ELSE Select 0 AS NewRowID ')",
                                    "type": "Expression"
                                },
                                "partitionOption": "None"
                            },
                            "dataset": {
                                "referenceName": "LSInsightDW",
                                "type": "DatasetReference",
                                "parameters": {
                                    "DestTableName": "NA"
                                }
                            }
                        }
                    },
                    {
                        "name": "Current-RowID-TSE-Error",
                        "description": "Stored procedure activity to store the new Pipeline run metadata in LSAudit table",
                        "type": "SqlServerStoredProcedure",
                        "dependsOn": [
                            {
                                "activity": "Current-RowID",
                                "dependencyConditions": [
                                    "Failed"
                                ]
                            }
                        ],
                        "policy": {
                            "timeout": "7.00:00:00",
                            "retry": 0,
                            "retryIntervalInSeconds": 30
                        },
                        "userProperties": [],
                        "typeProperties": {
                            "storedProcedureName": "[[dbo].[UpdateLSInsightAudit]",
                            "storedProcedureParameters": {
                                "ADFRunGUID": {
                                    "value": {
                                        "value": "@pipeline().RunId",
                                        "type": "Expression"
                                    },
                                    "type": "String"
                                },
                                "CopyDuration": {
                                    "value": {
                                        "value": "0",
                                        "type": "Expression"
                                    },
                                    "type": "Int32"
                                },
                                "PipelineName": {
                                    "value": {
                                        "value": "@concat('Failed Run - ',  pipeline().Pipeline)",
                                        "type": "Expression"
                                    },
                                    "type": "String"
                                },
                                "PipelineStart": {
                                    "value": {
                                        "value": "@variables('PipelineStart')",
                                        "type": "Expression"
                                    },
                                    "type": "DateTime"
                                },
                                "RowsCopied": {
                                    "value": {
                                        "value": "0",
                                        "type": "Expression"
                                    },
                                    "type": "Int32"
                                },
                                "RowsRead": {
                                    "value": {
                                        "value": "0",
                                        "type": "Expression"
                                    },
                                    "type": "Int32"
                                },
                                "RowVersion": {
                                    "value": {
                                        "value": "0",
                                        "type": "Expression"
                                    },
                                    "type": "Int64"
                                },
                                "Table": {
                                    "value": {
                                        "value": "@variables('DestinationTable')",
                                        "type": "Expression"
                                    },
                                    "type": "String"
                                },
                                "PipelineTriggerType": {
                                    "value": {
                                        "value": "@pipeline().TriggerType",
                                        "type": "Expression"
                                    },
                                    "type": "String"
                                },
                                "ErrorMessage": {
                                    "value": {
                                        "value": "@activity('Current-RowID').output.Error.Message",
                                        "type": "Expression"
                                    },
                                    "type": "string"
                                },
                                "Note": {
                                    "value": "TSE"
                                }
                            }
                        },
                        "linkedServiceName": {
                            "referenceName": "LSInsightDW",
                            "type": "LinkedServiceReference"
                        }
                    }
                ],
                "parameters": {
                    "Data_Source_WaterMarkColumn": {
                        "type": "String",
                        "defaultValue": "[[bigint_timestamp]"
                    },
                    "Control_Table_Table_Name": {
                        "type": "String",
                        "defaultValue": "[[dbo].[LSInsightAudit]"
                    },
                    "Control_Table_Column_Name": {
                        "type": "String",
                        "defaultValue": "[[NewRowVersion]"
                    },
                    "TablePrefix": {
                        "type": "string",
                        "defaultValue": "[parameters('TablePrefix')]"
                    }
                },
                "variables": {
                    "PipelineStart": {
                        "type": "String"
                    },
                    "DestinationTable": {
                        "type": "String",
                        "defaultValue": "[[DW].[fInventory]"
                    },
                    "Data_Source_Table_Name": {
                        "type": "String",
                        "defaultValue": "[[Stg$Item Ledger Entry]"
                    },
                    "ErrorMessage": {
                        "type": "String"
                    }
                },
                "folder": {
                    "name": "4 - Fact Tables"
                },
                "annotations": []
            },
            "dependsOn": [
                "[concat(variables('factoryId'), '/linkedServices/LSInsightDW')]",
                "[concat(variables('factoryId'), '/datasets/LSInsightDW')]"  
            ]
        },




        {
            "name": "[concat(parameters('factoryName'), '/PL-SP-factFraudEvent')]",
            "type": "Microsoft.DataFactory/factories/pipelines",
            "apiVersion": "2018-06-01",
            "properties": {
                "description": "Execute Stored Procedure in LS Insight DW",
                "activities": [
            {
                "name": "Last-RowID",
                "description": "Retrieve  the max rowversion value stored in the control table",
                "type": "Lookup",
                "dependsOn": [
                    {
                        "activity": "PipelineStart",
                        "dependencyConditions": [
                            "Succeeded"
                        ]
                    }
                ],
                "policy": {
                    "timeout": "7.00:00:00",
                    "retry": 0,
                    "retryIntervalInSeconds": 30
                },
                "userProperties": [],
                "typeProperties": {
                    "source": {
                        "type": "AzureSqlSource",
                        "sqlReaderQuery": {
                            "value": "@concat('SELECT COALESCE(max([NewRowVersion]),0)  AS LastRowID \n  FROM ', pipeline().parameters.Control_Table_Table_Name, '\n  where [Table] =''',variables('DestinationTable'),'''')",
                            "type": "Expression"
                        },
                        "partitionOption": "None"
                    },
                    "dataset": {
                        "referenceName": "LSInsightDW",
                        "type": "DatasetReference",
                        "parameters": {
                            "DestTableName": "NA"
                        }
                    },
                    "firstRowOnly": true
                }
            },
            {
                "name": "UpdateLSInsightAudit-TSE",
                "description": "Stored procedure activity to store the new Pipeline run metadata in LSAudit table",
                "type": "SqlServerStoredProcedure",
                "dependsOn": [
                    {
                        "activity": "Execute Stored Procedure",
                        "dependencyConditions": [
                            "Succeeded"
                        ]
                    }
                ],
                "policy": {
                    "timeout": "7.00:00:00",
                    "retry": 0,
                    "retryIntervalInSeconds": 30
                },
                "userProperties": [],
                "typeProperties": {
                    "storedProcedureName": "[[dbo].[UpdateLSInsightAudit]",
                    "storedProcedureParameters": {
                        "ADFRunGUID": {
                            "value": {
                                "value": "@pipeline().RunId",
                                "type": "Expression"
                            },
                            "type": "String"
                        },
                        "CopyDuration": {
                            "value": null,
                            "type": "Int32"
                        },
                        "PipelineName": {
                            "value": {
                                "value": "@pipeline().Pipeline",
                                "type": "Expression"
                            },
                            "type": "String"
                        },
                        "PipelineStart": {
                            "value": {
                                "value": "@variables('PipelineStart')",
                                "type": "Expression"
                            },
                            "type": "DateTime"
                        },
                        "RowsCopied": {
                            "value": {
                                "value": "@activity('Execute Stored Procedure').output.firstRow.RowCount",
                                "type": "Expression"
                            },
                            "type": "Int32"
                        },
                        "RowsRead": {
                            "value": null,
                            "type": "Int32"
                        },
                        "RowVersion": {
                            "value": {
                                "value": "@activity('Current-RowID').output.firstRow.NewRowID",
                                "type": "Expression"
                            },
                            "type": "Int64"
                        },
                        "Table": {
                            "value": {
                                "value": "@variables('DestinationTable')",
                                "type": "Expression"
                            },
                            "type": "String"
                        },
                        "PipelineTriggerType": {
                            "value": {
                                "value": "@pipeline().TriggerType",
                                "type": "Expression"
                            },
                            "type": "String"
                        },
                        "ErrorMessage": {
                            "value": null,
                            "type": "String"
                        },
                        "Note": {
                            "value": null
                        }
                    }
                },
                "linkedServiceName": {
                    "referenceName": "LSInsightDW",
                    "type": "LinkedServiceReference"
                }
            },
            {
                "name": "PipelineStart",
                "type": "SetVariable",
                "dependsOn": [],
                "userProperties": [],
                "typeProperties": {
                    "variableName": "PipelineStart",
                    "value": {
                        "value": "@utcnow()",
                        "type": "Expression"
                    }
                }
            },
            {
                "name": "SqlServerStoredProcedure1",
                "description": "Stored procedure activity to store the new Pipeline run metadata in LSAudit table",
                "type": "SqlServerStoredProcedure",
                "dependsOn": [
                    {
                        "activity": "Execute Stored Procedure",
                        "dependencyConditions": [
                            "Failed"
                        ]
                    }
                ],
                "policy": {
                    "timeout": "7.00:00:00",
                    "retry": 0,
                    "retryIntervalInSeconds": 30
                },
                "userProperties": [],
                "typeProperties": {
                    "storedProcedureName": "[[dbo].[UpdateLSInsightAudit]",
                    "storedProcedureParameters": {
                        "ADFRunGUID": {
                            "value": {
                                "value": "@pipeline().RunId",
                                "type": "Expression"
                            },
                            "type": "String"
                        },
                        "CopyDuration": {
                            "value": {
                                "value": "0",
                                "type": "Expression"
                            },
                            "type": "Int32"
                        },
                        "PipelineName": {
                            "value": {
                                "value": "@concat('Failed Run - ',  pipeline().Pipeline)",
                                "type": "Expression"
                            },
                            "type": "String"
                        },
                        "PipelineStart": {
                            "value": {
                                "value": "@variables('PipelineStart')",
                                "type": "Expression"
                            },
                            "type": "DateTime"
                        },
                        "RowsCopied": {
                            "value": {
                                "value": "0",
                                "type": "Expression"
                            },
                            "type": "Int32"
                        },
                        "RowsRead": {
                            "value": {
                                "value": "0",
                                "type": "Expression"
                            },
                            "type": "Int32"
                        },
                        "RowVersion": {
                            "value": {
                                "value": "0",
                                "type": "Expression"
                            },
                            "type": "Int64"
                        },
                        "Table": {
                            "value": {
                                "value": "@variables('Data_Source_Table_Name')",
                                "type": "Expression"
                            },
                            "type": "String"
                        },
                        "PipelineTriggerType": {
                            "value": {
                                "value": "@pipeline().TriggerType",
                                "type": "Expression"
                            },
                            "type": "String"
                        },
                        "ErrorMessage": {
                            "value": {
                                "value": "@activity('Execute Stored Procedure').error.message",
                                "type": "Expression"
                            },
                            "type": "string"
                        }
                    }
                },
                "linkedServiceName": {
                    "referenceName": "LSInsightDW",
                    "type": "LinkedServiceReference"
                }
            },
            {
                "name": "Last-RowID-Error",
                "description": "Stored procedure activity to store the new Pipeline run metadata in LSAudit table",
                "type": "SqlServerStoredProcedure",
                "dependsOn": [
                    {
                        "activity": "Last-RowID",
                        "dependencyConditions": [
                            "Failed"
                        ]
                    }
                ],
                "policy": {
                    "timeout": "7.00:00:00",
                    "retry": 0,
                    "retryIntervalInSeconds": 30
                },
                "userProperties": [],
                "typeProperties": {
                    "storedProcedureName": "[[dbo].[UpdateLSInsightAudit]",
                    "storedProcedureParameters": {
                        "ADFRunGUID": {
                            "value": {
                                "value": "@pipeline().RunId",
                                "type": "Expression"
                            },
                            "type": "String"
                        },
                        "CopyDuration": {
                            "value": {
                                "value": "0",
                                "type": "Expression"
                            },
                            "type": "Int32"
                        },
                        "PipelineName": {
                            "value": {
                                "value": "@concat('Failed Run - ',  pipeline().Pipeline)",
                                "type": "Expression"
                            },
                            "type": "String"
                        },
                        "PipelineStart": {
                            "value": {
                                "value": "@variables('PipelineStart')",
                                "type": "Expression"
                            },
                            "type": "DateTime"
                        },
                        "RowsCopied": {
                            "value": {
                                "value": "0",
                                "type": "Expression"
                            },
                            "type": "Int32"
                        },
                        "RowsRead": {
                            "value": {
                                "value": "0",
                                "type": "Expression"
                            },
                            "type": "Int32"
                        },
                        "RowVersion": {
                            "value": {
                                "value": "0",
                                "type": "Expression"
                            },
                            "type": "Int64"
                        },
                        "Table": {
                            "value": {
                                "value": "@variables('DestinationTable')",
                                "type": "Expression"
                            },
                            "type": "String"
                        },
                        "PipelineTriggerType": {
                            "value": {
                                "value": "@pipeline().TriggerType",
                                "type": "Expression"
                            },
                            "type": "String"
                        },
                        "ErrorMessage": {
                            "value": {
                                "value": "@activity('Last-RowID').output.Error.Message",
                                "type": "Expression"
                            },
                            "type": "string"
                        },
                        "Note": {
                            "value": ""
                        }
                    }
                },
                "linkedServiceName": {
                    "referenceName": "LSInsightDW",
                    "type": "LinkedServiceReference"
                }
            },
            {
                "name": "Execute Stored Procedure",
                "type": "Lookup",
                "dependsOn": [
                    {
                        "activity": "Last-RowID",
                        "dependencyConditions": [
                            "Succeeded"
                        ]
                    },
                    {
                        "activity": "Current-RowID",
                        "dependencyConditions": [
                            "Succeeded"
                        ]
                    }
                ],
                "policy": {
                    "timeout": "7.00:00:00",
                    "retry": 0,
                    "retryIntervalInSeconds": 30,
                    "secureOutput": false,
                    "secureInput": false
                },
                "userProperties": [],
                "typeProperties": {
                    "source": {
                        "type": "AzureSqlSource",
                        "sqlReaderStoredProcedureName": "[[dbo].[factFraudEvent]",
                        "storedProcedureParameters": {
                            "FraudEventNewRowID": {
                                "type": "Int64",
                                "value": {
                                    "value": "@activity('Current-RowID').output.firstRow.NewRowID",
                                    "type": "Expression"
                                }
                            },
                            "FraudEventRowID": {
                                "type": "Int64",
                                "value": {
                                    "value": "@activity('Last-RowID').output.firstRow.LastRowID",
                                    "type": "Expression"
                                }
                            }
                        },
                        "queryTimeout": "23:00:00",
                        "partitionOption": "None"
                    },
                    "dataset": {
                        "referenceName": "LSInsightDW",
                        "type": "DatasetReference",
                        "parameters": {
                            "DestTableName": "NA"
                        }
                    }
                }
            },
            {
                "name": "Current-RowID",
                "description": "Retrieve  the current maximum value in watermark column of source data store",
                "type": "Lookup",
                "dependsOn": [],
                "policy": {
                    "timeout": "7.00:00:00",
                    "retry": 0,
                    "retryIntervalInSeconds": 30
                },
                "userProperties": [],
                "typeProperties": {
                    "source": {
                        "type": "AzureSqlSource",
                        "sqlReaderQuery": {
                            "value": "@concat('IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(''',variables('Data_Source_Table_Name'),'''))  select MAX(convert(bigint, ',pipeline().parameters.Data_Source_WaterMarkColumn, ')) as NewRowID from ',variables('Data_Source_Table_Name'), ' ELSE Select 0 AS NewRowID ')",
                            "type": "Expression"
                        },
                        "partitionOption": "None"
                    },
                    "dataset": {
                        "referenceName": "LSInsightDW",
                        "type": "DatasetReference",
                        "parameters": {
                            "DestTableName": "NA"
                        }
                    }
                }
            },
            {
                "name": "Current-RowID-Error",
                "description": "Stored procedure activity to store the new Pipeline run metadata in LSAudit table",
                "type": "SqlServerStoredProcedure",
                "dependsOn": [
                    {
                        "activity": "Current-RowID",
                        "dependencyConditions": [
                            "Failed"
                        ]
                    }
                ],
                "policy": {
                    "timeout": "7.00:00:00",
                    "retry": 0,
                    "retryIntervalInSeconds": 30
                },
                "userProperties": [],
                "typeProperties": {
                    "storedProcedureName": "[[dbo].[UpdateLSInsightAudit]",
                    "storedProcedureParameters": {
                        "ADFRunGUID": {
                            "value": {
                                "value": "@pipeline().RunId",
                                "type": "Expression"
                            },
                            "type": "String"
                        },
                        "CopyDuration": {
                            "value": {
                                "value": "0",
                                "type": "Expression"
                            },
                            "type": "Int32"
                        },
                        "PipelineName": {
                            "value": {
                                "value": "@concat('Failed Run - ',  pipeline().Pipeline)",
                                "type": "Expression"
                            },
                            "type": "String"
                        },
                        "PipelineStart": {
                            "value": {
                                "value": "@variables('PipelineStart')",
                                "type": "Expression"
                            },
                            "type": "DateTime"
                        },
                        "RowsCopied": {
                            "value": {
                                "value": "0",
                                "type": "Expression"
                            },
                            "type": "Int32"
                        },
                        "RowsRead": {
                            "value": {
                                "value": "0",
                                "type": "Expression"
                            },
                            "type": "Int32"
                        },
                        "RowVersion": {
                            "value": {
                                "value": "0",
                                "type": "Expression"
                            },
                            "type": "Int64"
                        },
                        "Table": {
                            "value": {
                                "value": "@variables('DestinationTable')",
                                "type": "Expression"
                            },
                            "type": "String"
                        },
                        "PipelineTriggerType": {
                            "value": {
                                "value": "@pipeline().TriggerType",
                                "type": "Expression"
                            },
                            "type": "String"
                        },
                        "ErrorMessage": {
                            "value": {
                                "value": "@activity('Current-RowID').output.Error.Message",
                                "type": "Expression"
                            },
                            "type": "string"
                        },
                        "Note": {
                            "value": ""
                        }
                    }
                },
                "linkedServiceName": {
                    "referenceName": "LSInsightDW",
                    "type": "LinkedServiceReference"
                }
            }
        ],
        "parameters": {
            "Data_Source_WaterMarkColumn": {
                "type": "String",
                "defaultValue": "[[bigint_timestamp]"
            },
            "Control_Table_Table_Name": {
                "type": "String",
                "defaultValue": "[[dbo].[LSInsightAudit]"
            },
            "Control_Table_Column_Name": {
                "type": "String",
                "defaultValue": "[[NewRowVersion]"
            },
            "TablePrefix": {
                "type": "string",
                "defaultValue": "PLACEHOLDER-TABLEPREFIX"
            }
        },
        "variables": {
            "PipelineStart": {
                "type": "String"
            },
            "DestinationTable": {
                "type": "String",
                "defaultValue": "[[DW].[FactFraudEvent]"
            },
            "Data_Source_Table_Name": {
                "type": "String",
                "defaultValue": "[[stg$Fraud Event]"
            },
            "ErrorMessage": {
                "type": "String"
            }
        },
                "folder": {
                    "name": "4 - Fact Tables"
                },
                "annotations": []
            },
            "dependsOn": [
                "[concat(variables('factoryId'), '/linkedServices/LSInsightDW')]",
                "[concat(variables('factoryId'), '/datasets/LSInsightDW')]"  
            ]
        },
{
    "name": "[concat(parameters('factoryName'), '/PL-SP-factACIAlerts')]",
	"type": "Microsoft.DataFactory/factories/pipelines",
	"apiVersion": "2018-06-01",
    "properties": {
        "description": "Execute Stored Procedure in LS Insight DW",
        "activities": [
            {
                "name": "Last-RowID",
                "description": "Retrieve  the max rowversion value stored in the control table",
                "type": "Lookup",
                "dependsOn": [
                    {
                        "activity": "PipelineStart",
                        "dependencyConditions": [
                            "Succeeded"
                        ]
                    }
                ],
                "policy": {
                    "timeout": "7.00:00:00",
                    "retry": 0,
                    "retryIntervalInSeconds": 30
                },
                "userProperties": [],
                "typeProperties": {
                    "source": {
                        "type": "AzureSqlSource",
                        "sqlReaderQuery": {
                            "value": "@concat('SELECT COALESCE(max([NewRowVersion]),0)  AS LastRowID \n  FROM ', pipeline().parameters.Control_Table_Table_Name, '\n  where [Table] =''',variables('DestinationTable'),'''')",
                            "type": "Expression"
                        },
                        "partitionOption": "None"
                    },
                    "dataset": {
                        "referenceName": "LSInsightDW",
                        "type": "DatasetReference",
                        "parameters": {
                            "DestTableName": "NA"
                        }
                    },
                    "firstRowOnly": true
                }
            },
            {
                "name": "UpdateLSInsightAudit-Alerts",
                "description": "Stored procedure activity to store the new Pipeline run metadata in LSAudit table",
                "type": "SqlServerStoredProcedure",
                "dependsOn": [
                    {
                        "activity": "Execute Stored Procedure",
                        "dependencyConditions": [
                            "Succeeded"
                        ]
                    }
                ],
                "policy": {
                    "timeout": "7.00:00:00",
                    "retry": 0,
                    "retryIntervalInSeconds": 30
                },
                "userProperties": [],
                "typeProperties": {
                    "storedProcedureName": "[[dbo].[UpdateLSInsightAudit]",
                    "storedProcedureParameters": {
                        "ADFRunGUID": {
                            "value": {
                                "value": "@pipeline().RunId",
                                "type": "Expression"
                            },
                            "type": "String"
                        },
                        "CopyDuration": {
                            "value": null,
                            "type": "Int32"
                        },
                        "PipelineName": {
                            "value": {
                                "value": "@pipeline().Pipeline",
                                "type": "Expression"
                            },
                            "type": "String"
                        },
                        "PipelineStart": {
                            "value": {
                                "value": "@variables('PipelineStart')",
                                "type": "Expression"
                            },
                            "type": "DateTime"
                        },
                        "RowsCopied": {
                            "value": {
                                "value": "@activity('Execute Stored Procedure').output.firstRow.RowCount",
                                "type": "Expression"
                            },
                            "type": "Int32"
                        },
                        "RowsRead": {
                            "value": null,
                            "type": "Int32"
                        },
                        "RowVersion": {
                            "value": {
                                "value": "@activity('Current-RowID').output.firstRow.NewRowID",
                                "type": "Expression"
                            },
                            "type": "Int64"
                        },
                        "Table": {
                            "value": {
                                "value": "@variables('DestinationTable')",
                                "type": "Expression"
                            },
                            "type": "String"
                        },
                        "PipelineTriggerType": {
                            "value": {
                                "value": "@pipeline().TriggerType",
                                "type": "Expression"
                            },
                            "type": "String"
                        },
                        "ErrorMessage": {
                            "value": null,
                            "type": "String"
                        },
                        "Note": {
                            "value": null
                        }
                    }
                },
                "linkedServiceName": {
                    "referenceName": "LSInsightDW",
                    "type": "LinkedServiceReference"
                   
                }
            },
            {
                "name": "PipelineStart",
                "type": "SetVariable",
                "dependsOn": [],
                "userProperties": [],
                "typeProperties": {
                    "variableName": "PipelineStart",
                    "value": {
                        "value": "@utcnow()",
                        "type": "Expression"
                    }
                }
            },
            {
                "name": "SqlServerStoredProcedure1",
                "description": "Stored procedure activity to store the new Pipeline run metadata in LSAudit table",
                "type": "SqlServerStoredProcedure",
                "dependsOn": [
                    {
                        "activity": "Execute Stored Procedure",
                        "dependencyConditions": [
                            "Failed"
                        ]
                    }
                ],
                "policy": {
                    "timeout": "7.00:00:00",
                    "retry": 0,
                    "retryIntervalInSeconds": 30
                },
                "userProperties": [],
                "typeProperties": {
                    "storedProcedureName": "[[dbo].[UpdateLSInsightAudit]",
                    "storedProcedureParameters": {
                        "ADFRunGUID": {
                            "value": {
                                "value": "@pipeline().RunId",
                                "type": "Expression"
                            },
                            "type": "String"
                        },
                        "CopyDuration": {
                            "value": {
                                "value": "0",
                                "type": "Expression"
                            },
                            "type": "Int32"
                        },
                        "PipelineName": {
                            "value": {
                                "value": "@concat('Failed Run - ',  pipeline().Pipeline)",
                                "type": "Expression"
                            },
                            "type": "String"
                        },
                        "PipelineStart": {
                            "value": {
                                "value": "@variables('PipelineStart')",
                                "type": "Expression"
                            },
                            "type": "DateTime"
                        },
                        "RowsCopied": {
                            "value": {
                                "value": "0",
                                "type": "Expression"
                            },
                            "type": "Int32"
                        },
                        "RowsRead": {
                            "value": {
                                "value": "0",
                                "type": "Expression"
                            },
                            "type": "Int32"
                        },
                        "RowVersion": {
                            "value": {
                                "value": "0",
                                "type": "Expression"
                            },
                            "type": "Int64"
                        },
                        "Table": {
                            "value": {
                                "value": "@variables('Data_Source_Table_Name')",
                                "type": "Expression"
                            },
                            "type": "String"
                        },
                        "PipelineTriggerType": {
                            "value": {
                                "value": "@pipeline().TriggerType",
                                "type": "Expression"
                            },
                            "type": "String"
                        },
                        "ErrorMessage": {
                            "value": {
                                "value": "@activity('Execute Stored Procedure').error.message",
                                "type": "Expression"
                            },
                            "type": "string"
                        }
                    }
                },
                "linkedServiceName": {
                    "referenceName": "LSInsightDW",
                    "type": "LinkedServiceReference"
                }
            },
            {
                "name": "Last-Alerts-RowID-Error",
                "description": "Stored procedure activity to store the new Pipeline run metadata in LSAudit table",
                "type": "SqlServerStoredProcedure",
                "dependsOn": [
                    {
                        "activity": "Last-RowID",
                        "dependencyConditions": [
                            "Failed"
                        ]
                    }
                ],
                "policy": {
                    "timeout": "7.00:00:00",
                    "retry": 0,
                    "retryIntervalInSeconds": 30
                },
                "userProperties": [],
                "typeProperties": {
                    "storedProcedureName": "[[dbo].[UpdateLSInsightAudit]",
                    "storedProcedureParameters": {
                        "ADFRunGUID": {
                            "value": {
                                "value": "@pipeline().RunId",
                                "type": "Expression"
                            },
                            "type": "String"
                        },
                        "CopyDuration": {
                            "value": {
                                "value": "0",
                                "type": "Expression"
                            },
                            "type": "Int32"
                        },
                        "PipelineName": {
                            "value": {
                                "value": "@concat('Failed Run - ',  pipeline().Pipeline)",
                                "type": "Expression"
                            },
                            "type": "String"
                        },
                        "PipelineStart": {
                            "value": {
                                "value": "@variables('PipelineStart')",
                                "type": "Expression"
                            },
                            "type": "DateTime"
                        },
                        "RowsCopied": {
                            "value": {
                                "value": "0",
                                "type": "Expression"
                            },
                            "type": "Int32"
                        },
                        "RowsRead": {
                            "value": {
                                "value": "0",
                                "type": "Expression"
                            },
                            "type": "Int32"
                        },
                        "RowVersion": {
                            "value": {
                                "value": "0",
                                "type": "Expression"
                            },
                            "type": "Int64"
                        },
                        "Table": {
                            "value": {
                                "value": "@variables('DestinationTable')",
                                "type": "Expression"
                            },
                            "type": "String"
                        },
                        "PipelineTriggerType": {
                            "value": {
                                "value": "@pipeline().TriggerType",
                                "type": "Expression"
                            },
                            "type": "String"
                        },
                        "ErrorMessage": {
                            "value": {
                                "value": "@activity('Last-RowID').output.Error.Message",
                                "type": "Expression"
                            },
                            "type": "string"
                        },
                        "Note": {
                            "value": null
                        }
                    }
                },
                "linkedServiceName": {
                    "referenceName": "LSInsightDW",
                    "type": "LinkedServiceReference"
                }
            },
            {
                "name": "Execute Stored Procedure",
                "type": "Lookup",
                "dependsOn": [
                    {
                        "activity": "Last-RowID",
                        "dependencyConditions": [
                            "Succeeded"
                        ]
                    },
                    {
                        "activity": "Current-RowID",
                        "dependencyConditions": [
                            "Succeeded"
                        ]
                    }
                ],
                "policy": {
                    "timeout": "7.00:00:00",
                    "retry": 0,
                    "retryIntervalInSeconds": 30,
                    "secureOutput": false,
                    "secureInput": false
                },
                "userProperties": [],
                "typeProperties": {
                    "source": {
                        "type": "AzureSqlSource",
                        "sqlReaderStoredProcedureName": "[[dbo].[factACIAlerts]",
                        "storedProcedureParameters": {
                            "AlertDetailNewRowID": {
                                "type": "Int64",
                                "value": {
                                    "value": "@activity('Current-RowID').output.firstRow.NewRowID",
                                    "type": "Expression"
                                }
                            },
                            "AlertDetailRowID": {
                                "type": "Int64",
                                "value": {
                                    "value": "@activity('Last-RowID').output.firstRow.LastRowID",
                                    "type": "Expression"
                                }
                            }
                        },
                        "queryTimeout": "23:00:00",
                        "partitionOption": "None"
                    },
                    "dataset": {
                        "referenceName": "LSInsightDW",
                        "type": "DatasetReference",
                        "parameters": {
                            "DestTableName": "NA"
                        }
                    }
                }
            },
            {
                "name": "Current-RowID",
                "description": "Retrieve  the current maximum value in watermark column of source data store",
                "type": "Lookup",
                "dependsOn": [],
                "policy": {
                    "timeout": "7.00:00:00",
                    "retry": 0,
                    "retryIntervalInSeconds": 30
                },
                "userProperties": [],
                "typeProperties": {
                    "source": {
                        "type": "AzureSqlSource",
                        "sqlReaderQuery": {
                            "value": "@concat('IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(''',variables('Data_Source_Table_Name'),'''))  select MAX(convert(bigint, ',pipeline().parameters.Data_Source_WaterMarkColumn, ')) as NewRowID from ',variables('Data_Source_Table_Name'), ' ELSE Select 0 AS NewRowID ')",
                            "type": "Expression"
                        },
                        "partitionOption": "None"
                    },
                    "dataset": {
                        "referenceName": "LSInsightDW",
                        "type": "DatasetReference",
                        "parameters": {
                            "DestTableName": "NA"
                        }
                    }
                }
            },
            {
                "name": "Current-RowID-Alerts-Error",
                "description": "Stored procedure activity to store the new Pipeline run metadata in LSAudit table",
                "type": "SqlServerStoredProcedure",
                "dependsOn": [
                    {
                        "activity": "Current-RowID",
                        "dependencyConditions": [
                            "Failed"
                        ]
                    }
                ],
                "policy": {
                    "timeout": "7.00:00:00",
                    "retry": 0,
                    "retryIntervalInSeconds": 30
                },
                "userProperties": [],
                "typeProperties": {
                    "storedProcedureName": "[[dbo].[UpdateLSInsightAudit]",
                    "storedProcedureParameters": {
                        "ADFRunGUID": {
                            "value": {
                                "value": "@pipeline().RunId",
                                "type": "Expression"
                            },
                            "type": "String"
                        },
                        "CopyDuration": {
                            "value": {
                                "value": "0",
                                "type": "Expression"
                            },
                            "type": "Int32"
                        },
                        "PipelineName": {
                            "value": {
                                "value": "@concat('Failed Run - ',  pipeline().Pipeline)",
                                "type": "Expression"
                            },
                            "type": "String"
                        },
                        "PipelineStart": {
                            "value": {
                                "value": "@variables('PipelineStart')",
                                "type": "Expression"
                            },
                            "type": "DateTime"
                        },
                        "RowsCopied": {
                            "value": {
                                "value": "0",
                                "type": "Expression"
                            },
                            "type": "Int32"
                        },
                        "RowsRead": {
                            "value": {
                                "value": "0",
                                "type": "Expression"
                            },
                            "type": "Int32"
                        },
                        "RowVersion": {
                            "value": {
                                "value": "0",
                                "type": "Expression"
                            },
                            "type": "Int64"
                        },
                        "Table": {
                            "value": {
                                "value": "@variables('DestinationTable')",
                                "type": "Expression"
                            },
                            "type": "String"
                        },
                        "PipelineTriggerType": {
                            "value": {
                                "value": "@pipeline().TriggerType",
                                "type": "Expression"
                            },
                            "type": "String"
                        },
                        "ErrorMessage": {
                            "value": {
                                "value": "@activity('Current-RowID').output.Error.Message",
                                "type": "Expression"
                            },
                            "type": "string"
                        },
                        "Note": {
                            "value": null
                        }
                    }
                },
                "linkedServiceName": {
                    "referenceName": "LSInsightDW",
                    "type": "LinkedServiceReference"
                }
            }
        ],
        "parameters": {
            "Data_Source_WaterMarkColumn": {
                "type": "String",
                "defaultValue": "[[bigint_timestamp]"
            },
            "Control_Table_Table_Name": {
                "type": "String",
                "defaultValue": "[[dbo].[LSInsightAudit]"
            },
            "Control_Table_Column_Name": {
                "type": "String",
                "defaultValue": "[[NewRowVersion]"
            }
        },
        "variables": {
            "PipelineStart": {
                "type": "String"
            },
            "DestinationTable": {
                "type": "String",
                "defaultValue": "[[DW].[fACIAlerts]"
            },
            "Data_Source_Table_Name": {
                "type": "String",
                "defaultValue": "[[stg$ACI Alert Detail]"
            },
            "ErrorMessage": {
                "type": "String"
            }
        },
        "folder": {
            "name": "4 - Fact Tables"
        },
        "annotations": []
    },
            "dependsOn": [
                "[concat(variables('factoryId'), '/linkedServices/LSInsightDW')]",
                "[concat(variables('factoryId'), '/datasets/LSInsightDW')]"  
            ]
} ,
{   "name": "[concat(parameters('factoryName'), '/PL-SP-factACISignals')]",
	"type": "Microsoft.DataFactory/factories/pipelines",
	"apiVersion": "2018-06-01",
    "properties": {
        "description": "Execute Stored Procedure in LS Insight DW",
        "activities": [
            {
                "name": "Last-RowID",
                "description": "Retrieve  the max rowversion value stored in the control table",
                "type": "Lookup",
                "dependsOn": [
                    {
                        "activity": "PipelineStart",
                        "dependencyConditions": [
                            "Succeeded"
                        ]
                    }
                ],
                "policy": {
                    "timeout": "7.00:00:00",
                    "retry": 0,
                    "retryIntervalInSeconds": 30
                },
                "userProperties": [],
                "typeProperties": {
                    "source": {
                        "type": "AzureSqlSource",
                        "sqlReaderQuery": {
                            "value": "@concat('SELECT COALESCE(max([NewRowVersion]),0)  AS LastRowID \n  FROM ', pipeline().parameters.Control_Table_Table_Name, '\n  where [Table] =''',variables('DestinationTable'),'''')",
                            "type": "Expression"
                        },
                        "partitionOption": "None"
                    },
                    "dataset": {
                        "referenceName": "LSInsightDW",
                        "type": "DatasetReference",
                        "parameters": {
                            "DestTableName": "NA"
                        }
                    },
                    "firstRowOnly": true
                }
            },
            {
                "name": "UpdateLSInsightAudit-Signals",
                "description": "Stored procedure activity to store the new Pipeline run metadata in LSAudit table",
                "type": "SqlServerStoredProcedure",
                "dependsOn": [
                    {
                        "activity": "Execute Stored Procedure",
                        "dependencyConditions": [
                            "Succeeded"
                        ]
                    }
                ],
                "policy": {
                    "timeout": "7.00:00:00",
                    "retry": 0,
                    "retryIntervalInSeconds": 30
                },
                "userProperties": [],
                "typeProperties": {
                    "storedProcedureName": "[[dbo].[UpdateLSInsightAudit]",
                    "storedProcedureParameters": {
                        "ADFRunGUID": {
                            "value": {
                                "value": "@pipeline().RunId",
                                "type": "Expression"
                            },
                            "type": "String"
                        },
                        "CopyDuration": {
                            "value": null,
                            "type": "Int32"
                        },
                        "PipelineName": {
                            "value": {
                                "value": "@pipeline().Pipeline",
                                "type": "Expression"
                            },
                            "type": "String"
                        },
                        "PipelineStart": {
                            "value": {
                                "value": "@variables('PipelineStart')",
                                "type": "Expression"
                            },
                            "type": "DateTime"
                        },
                        "RowsCopied": {
                            "value": {
                                "value": "@activity('Execute Stored Procedure').output.firstRow.RowCount",
                                "type": "Expression"
                            },
                            "type": "Int32"
                        },
                        "RowsRead": {
                            "value": null,
                            "type": "Int32"
                        },
                        "RowVersion": {
                            "value": {
                                "value": "@activity('Current-RowID').output.firstRow.NewRowID",
                                "type": "Expression"
                            },
                            "type": "Int64"
                        },
                        "Table": {
                            "value": {
                                "value": "@variables('DestinationTable')",
                                "type": "Expression"
                            },
                            "type": "String"
                        },
                        "PipelineTriggerType": {
                            "value": {
                                "value": "@pipeline().TriggerType",
                                "type": "Expression"
                            },
                            "type": "String"
                        },
                        "ErrorMessage": {
                            "value": null,
                            "type": "String"
                        },
                        "Note": {
                            "value": null
                        }
                    }
                },
                "linkedServiceName": {
                    "referenceName": "LSInsightDW",
                    "type": "LinkedServiceReference"
                }
            },
            {
                "name": "PipelineStart",
                "type": "SetVariable",
                "dependsOn": [],
                "userProperties": [],
                "typeProperties": {
                    "variableName": "PipelineStart",
                    "value": {
                        "value": "@utcnow()",
                        "type": "Expression"
                    }
                }
            },
            {
                "name": "SqlServerStoredProcedure1",
                "description": "Stored procedure activity to store the new Pipeline run metadata in LSAudit table",
                "type": "SqlServerStoredProcedure",
                "dependsOn": [
                    {
                        "activity": "Execute Stored Procedure",
                        "dependencyConditions": [
                            "Failed"
                        ]
                    }
                ],
                "policy": {
                    "timeout": "7.00:00:00",
                    "retry": 0,
                    "retryIntervalInSeconds": 30
                },
                "userProperties": [],
                "typeProperties": {
                    "storedProcedureName": "[[dbo].[UpdateLSInsightAudit]",
                    "storedProcedureParameters": {
                        "ADFRunGUID": {
                            "value": {
                                "value": "@pipeline().RunId",
                                "type": "Expression"
                            },
                            "type": "String"
                        },
                        "CopyDuration": {
                            "value": {
                                "value": "0",
                                "type": "Expression"
                            },
                            "type": "Int32"
                        },
                        "PipelineName": {
                            "value": {
                                "value": "@concat('Failed Run - ',  pipeline().Pipeline)",
                                "type": "Expression"
                            },
                            "type": "String"
                        },
                        "PipelineStart": {
                            "value": {
                                "value": "@variables('PipelineStart')",
                                "type": "Expression"
                            },
                            "type": "DateTime"
                        },
                        "RowsCopied": {
                            "value": {
                                "value": "0",
                                "type": "Expression"
                            },
                            "type": "Int32"
                        },
                        "RowsRead": {
                            "value": {
                                "value": "0",
                                "type": "Expression"
                            },
                            "type": "Int32"
                        },
                        "RowVersion": {
                            "value": {
                                "value": "0",
                                "type": "Expression"
                            },
                            "type": "Int64"
                        },
                        "Table": {
                            "value": {
                                "value": "@variables('Data_Source_Table_Name')",
                                "type": "Expression"
                            },
                            "type": "String"
                        },
                        "PipelineTriggerType": {
                            "value": {
                                "value": "@pipeline().TriggerType",
                                "type": "Expression"
                            },
                            "type": "String"
                        },
                        "ErrorMessage": {
                            "value": {
                                "value": "@activity('Execute Stored Procedure').error.message",
                                "type": "Expression"
                            },
                            "type": "string"
                        }
                    }
                },
                "linkedServiceName": {
                    "referenceName": "LSInsightDW",
                    "type": "LinkedServiceReference"
                }
            },
            {
                "name": "Last-Signals-RowID-Error",
                "description": "Stored procedure activity to store the new Pipeline run metadata in LSAudit table",
                "type": "SqlServerStoredProcedure",
                "dependsOn": [
                    {
                        "activity": "Last-RowID",
                        "dependencyConditions": [
                            "Failed"
                        ]
                    }
                ],
                "policy": {
                    "timeout": "7.00:00:00",
                    "retry": 0,
                    "retryIntervalInSeconds": 30
                },
                "userProperties": [],
                "typeProperties": {
                    "storedProcedureName": "[[dbo].[UpdateLSInsightAudit]",
                    "storedProcedureParameters": {
                        "ADFRunGUID": {
                            "value": {
                                "value": "@pipeline().RunId",
                                "type": "Expression"
                            },
                            "type": "String"
                        },
                        "CopyDuration": {
                            "value": {
                                "value": "0",
                                "type": "Expression"
                            },
                            "type": "Int32"
                        },
                        "PipelineName": {
                            "value": {
                                "value": "@concat('Failed Run - ',  pipeline().Pipeline)",
                                "type": "Expression"
                            },
                            "type": "String"
                        },
                        "PipelineStart": {
                            "value": {
                                "value": "@variables('PipelineStart')",
                                "type": "Expression"
                            },
                            "type": "DateTime"
                        },
                        "RowsCopied": {
                            "value": {
                                "value": "0",
                                "type": "Expression"
                            },
                            "type": "Int32"
                        },
                        "RowsRead": {
                            "value": {
                                "value": "0",
                                "type": "Expression"
                            },
                            "type": "Int32"
                        },
                        "RowVersion": {
                            "value": {
                                "value": "0",
                                "type": "Expression"
                            },
                            "type": "Int64"
                        },
                        "Table": {
                            "value": {
                                "value": "@variables('DestinationTable')",
                                "type": "Expression"
                            },
                            "type": "String"
                        },
                        "PipelineTriggerType": {
                            "value": {
                                "value": "@pipeline().TriggerType",
                                "type": "Expression"
                            },
                            "type": "String"
                        },
                        "ErrorMessage": {
                            "value": {
                                "value": "@activity('Last-RowID').output.Error.Message",
                                "type": "Expression"
                            },
                            "type": "string"
                        },
                        "Note": {
                            "value": null
                        }
                    }
                },
                "linkedServiceName": {
                    "referenceName": "LSInsightDW",
                    "type": "LinkedServiceReference"
                }
            },
            {
                "name": "Execute Stored Procedure",
                "type": "Lookup",
                "dependsOn": [
                    {
                        "activity": "Last-RowID",
                        "dependencyConditions": [
                            "Succeeded"
                        ]
                    },
                    {
                        "activity": "Current-RowID",
                        "dependencyConditions": [
                            "Succeeded"
                        ]
                    }
                ],
                "policy": {
                    "timeout": "7.00:00:00",
                    "retry": 0,
                    "retryIntervalInSeconds": 30,
                    "secureOutput": false,
                    "secureInput": false
                },
                "userProperties": [],
                "typeProperties": {
                    "source": {
                        "type": "AzureSqlSource",
                        "sqlReaderStoredProcedureName": "[[dbo].[factACISignals]",
                        "storedProcedureParameters": {
                            "ACISignalsNewRowID": {
                                "type": "Int64",
                                "value": {
                                    "value": "@activity('Current-RowID').output.firstRow.NewRowID",
                                    "type": "Expression"
                                }
                            },
                            "ACISignalsRowID": {
                                "type": "Int64",
                                "value": {
                                    "value": "@activity('Last-RowID').output.firstRow.LastRowID",
                                    "type": "Expression"
                                }
                            }
                        },
                        "queryTimeout": "23:00:00",
                        "partitionOption": "None"
                    },
                    "dataset": {
                        "referenceName": "LSInsightDW",
                        "type": "DatasetReference",
                        "parameters": {
                            "DestTableName": "NA"
                        }
                    }
                }
            },
            {
                "name": "Current-RowID",
                "description": "Retrieve  the current maximum value in watermark column of source data store",
                "type": "Lookup",
                "dependsOn": [],
                "policy": {
                    "timeout": "7.00:00:00",
                    "retry": 0,
                    "retryIntervalInSeconds": 30
                },
                "userProperties": [],
                "typeProperties": {
                    "source": {
                        "type": "AzureSqlSource",
                        "sqlReaderQuery": {
                            "value": "@concat('IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(''',variables('Data_Source_Table_Name'),'''))  select MAX(convert(bigint, ',pipeline().parameters.Data_Source_WaterMarkColumn, ')) as NewRowID from ',variables('Data_Source_Table_Name'), ' ELSE Select 0 AS NewRowID ')",
                            "type": "Expression"
                        },
                        "partitionOption": "None"
                    },
                    "dataset": {
                        "referenceName": "LSInsightDW",
                        "type": "DatasetReference",
                        "parameters": {
                            "DestTableName": "NA"
                        }
                    }
                }
            },
            {
                "name": "Current-Signals-Alerts-Error",
                "description": "Stored procedure activity to store the new Pipeline run metadata in LSAudit table",
                "type": "SqlServerStoredProcedure",
                "dependsOn": [
                    {
                        "activity": "Current-RowID",
                        "dependencyConditions": [
                            "Failed"
                        ]
                    }
                ],
                "policy": {
                    "timeout": "7.00:00:00",
                    "retry": 0,
                    "retryIntervalInSeconds": 30
                },
                "userProperties": [],
                "typeProperties": {
                    "storedProcedureName": "[[dbo].[UpdateLSInsightAudit]",
                    "storedProcedureParameters": {
                        "ADFRunGUID": {
                            "value": {
                                "value": "@pipeline().RunId",
                                "type": "Expression"
                            },
                            "type": "String"
                        },
                        "CopyDuration": {
                            "value": {
                                "value": "0",
                                "type": "Expression"
                            },
                            "type": "Int32"
                        },
                        "PipelineName": {
                            "value": {
                                "value": "@concat('Failed Run - ',  pipeline().Pipeline)",
                                "type": "Expression"
                            },
                            "type": "String"
                        },
                        "PipelineStart": {
                            "value": {
                                "value": "@variables('PipelineStart')",
                                "type": "Expression"
                            },
                            "type": "DateTime"
                        },
                        "RowsCopied": {
                            "value": {
                                "value": "0",
                                "type": "Expression"
                            },
                            "type": "Int32"
                        },
                        "RowsRead": {
                            "value": {
                                "value": "0",
                                "type": "Expression"
                            },
                            "type": "Int32"
                        },
                        "RowVersion": {
                            "value": {
                                "value": "0",
                                "type": "Expression"
                            },
                            "type": "Int64"
                        },
                        "Table": {
                            "value": {
                                "value": "@variables('DestinationTable')",
                                "type": "Expression"
                            },
                            "type": "String"
                        },
                        "PipelineTriggerType": {
                            "value": {
                                "value": "@pipeline().TriggerType",
                                "type": "Expression"
                            },
                            "type": "String"
                        },
                        "ErrorMessage": {
                            "value": {
                                "value": "@activity('Current-RowID').output.Error.Message",
                                "type": "Expression"
                            },
                            "type": "string"
                        },
                        "Note": {
                            "value": null
                        }
                    }
                },
                "linkedServiceName": {
                    "referenceName": "LSInsightDW",
                    "type": "LinkedServiceReference"
                }
            }
        ],
        "parameters": {
            "Data_Source_WaterMarkColumn": {
                "type": "String",
                "defaultValue": "[[bigint_timestamp]"
            },
            "Control_Table_Table_Name": {
                "type": "String",
                "defaultValue": "[[dbo].[LSInsightAudit]"
            },
            "Control_Table_Column_Name": {
                "type": "String",
                "defaultValue": "[[NewRowVersion]"
            }
        },
        "variables": {
            "PipelineStart": {
                "type": "String"
            },
            "DestinationTable": {
                "type": "String",
                "defaultValue": "[[DW].[fACISignals]"
            },
            "Data_Source_Table_Name": {
                "type": "String",
                "defaultValue": "[[stg$ACI Signal]"
            },
            "ErrorMessage": {
                "type": "String"
            }
        },
        "folder": {
            "name": "4 - Fact Tables"
        },
        "annotations": []
    },
            "dependsOn": [
                "[concat(variables('factoryId'), '/linkedServices/LSInsightDW')]",
                "[concat(variables('factoryId'), '/datasets/LSInsightDW')]" 
            ]
        },
        {
            "name": "[concat(parameters('factoryName'), '/PL-SP-factDiscount')]",
            "type": "Microsoft.DataFactory/factories/pipelines",
            "apiVersion": "2018-06-01",
            "properties": {
                "description": "Execute Stored Procedure in LS Insight DW",
                "activities": [
                    {
                        "name": "Last-RowID",
                        "description": "Retrieve  the max rowversion value stored in the control table",
                        "type": "Lookup",
                        "dependsOn": [
                            {
                                "activity": "PipelineStart",
                                "dependencyConditions": [
                                    "Succeeded"
                                ]
                            }
                        ],
                        "policy": {
                            "timeout": "7.00:00:00",
                            "retry": 0,
                            "retryIntervalInSeconds": 30
                        },
                        "userProperties": [],
                        "typeProperties": {
                            "source": {
                                "type": "AzureSqlSource",
                                "sqlReaderQuery": {
                                    "value": "@concat('SELECT COALESCE(max([NewRowVersion]),0)  AS LastRowID \n  FROM ', pipeline().parameters.Control_Table_Table_Name, '\n  where [Table] =''',variables('DestinationTable'),'''')",
                                    "type": "Expression"
                                },
                                "partitionOption": "None"
                            },
                            "dataset": {
                                "referenceName": "LSInsightDW",
                                "type": "DatasetReference",
                                "parameters": {
                                    "DestTableName": "NA"
                                }
                            },
                            "firstRowOnly": true
                        }
                    },
                    {
                        "name": "UpdateLSInsightAudit-TSE",
                        "description": "Stored procedure activity to store the new Pipeline run metadata in LSAudit table",
                        "type": "SqlServerStoredProcedure",
                        "dependsOn": [
                            {
                                "activity": "Execute Stored Procedure",
                                "dependencyConditions": [
                                    "Succeeded"
                                ]
                            }
                        ],
                        "policy": {
                            "timeout": "7.00:00:00",
                            "retry": 0,
                            "retryIntervalInSeconds": 30
                        },
                        "userProperties": [],
                        "typeProperties": {
                            "storedProcedureName": "[[dbo].[UpdateLSInsightAudit]",
                            "storedProcedureParameters": {
                                "ADFRunGUID": {
                                    "value": {
                                        "value": "@pipeline().RunId",
                                        "type": "Expression"
                                    },
                                    "type": "String"
                                },
                                "CopyDuration": {
                                    "value": null,
                                    "type": "Int32"
                                },
                                "PipelineName": {
                                    "value": {
                                        "value": "@pipeline().Pipeline",
                                        "type": "Expression"
                                    },
                                    "type": "String"
                                },
                                "PipelineStart": {
                                    "value": {
                                        "value": "@variables('PipelineStart')",
                                        "type": "Expression"
                                    },
                                    "type": "DateTime"
                                },
                                "RowsCopied": {
                                    "value": {
                                        "value": "@activity('Execute Stored Procedure').output.firstRow.RowCount",
                                        "type": "Expression"
                                    },
                                    "type": "Int32"
                                },
                                "RowsRead": {
                                    "value": null,
                                    "type": "Int32"
                                },
                                "RowVersion": {
                                    "value": {
                                        "value": "@activity('Current-RowID').output.firstRow.NewRowID",
                                        "type": "Expression"
                                    },
                                    "type": "Int64"
                                },
                                "Table": {
                                    "value": {
                                        "value": "@variables('DestinationTable')",
                                        "type": "Expression"
                                    },
                                    "type": "String"
                                },
                                "PipelineTriggerType": {
                                    "value": {
                                        "value": "@pipeline().TriggerType",
                                        "type": "Expression"
                                    },
                                    "type": "String"
                                },
                                "ErrorMessage": {
                                    "value": null,
                                    "type": "String"
                                },
                                "Note": {
                                    "value": null
                                }
                            }
                        },
                        "linkedServiceName": {
                            "referenceName": "LSInsightDW",
                            "type": "LinkedServiceReference"
                        }
                    },
                    {
                        "name": "PipelineStart",
                        "type": "SetVariable",
                        "dependsOn": [],
                        "userProperties": [],
                        "typeProperties": {
                            "variableName": "PipelineStart",
                            "value": {
                                "value": "@utcnow()",
                                "type": "Expression"
                            }
                        }
                    },
                    {
                        "name": "SqlServerStoredProcedure1",
                        "description": "Stored procedure activity to store the new Pipeline run metadata in LSAudit table",
                        "type": "SqlServerStoredProcedure",
                        "dependsOn": [
                            {
                                "activity": "Execute Stored Procedure",
                                "dependencyConditions": [
                                    "Failed"
                                ]
                            }
                        ],
                        "policy": {
                            "timeout": "7.00:00:00",
                            "retry": 0,
                            "retryIntervalInSeconds": 30
                        },
                        "userProperties": [],
                        "typeProperties": {
                            "storedProcedureName": "[[dbo].[UpdateLSInsightAudit]",
                            "storedProcedureParameters": {
                                "ADFRunGUID": {
                                    "value": {
                                        "value": "@pipeline().RunId",
                                        "type": "Expression"
                                    },
                                    "type": "String"
                                },
                                "CopyDuration": {
                                    "value": {
                                        "value": "0",
                                        "type": "Expression"
                                    },
                                    "type": "Int32"
                                },
                                "PipelineName": {
                                    "value": {
                                        "value": "@concat('Failed Run - ',  pipeline().Pipeline)",
                                        "type": "Expression"
                                    },
                                    "type": "String"
                                },
                                "PipelineStart": {
                                    "value": {
                                        "value": "@variables('PipelineStart')",
                                        "type": "Expression"
                                    },
                                    "type": "DateTime"
                                },
                                "RowsCopied": {
                                    "value": {
                                        "value": "0",
                                        "type": "Expression"
                                    },
                                    "type": "Int32"
                                },
                                "RowsRead": {
                                    "value": {
                                        "value": "0",
                                        "type": "Expression"
                                    },
                                    "type": "Int32"
                                },
                                "RowVersion": {
                                    "value": {
                                        "value": "0",
                                        "type": "Expression"
                                    },
                                    "type": "Int64"
                                },
                                "Table": {
                                    "value": {
                                        "value": "@variables('Data_Source_Table_Name')",
                                        "type": "Expression"
                                    },
                                    "type": "String"
                                },
                                "PipelineTriggerType": {
                                    "value": {
                                        "value": "@pipeline().TriggerType",
                                        "type": "Expression"
                                    },
                                    "type": "String"
                                },
                                "ErrorMessage": {
                                    "value": {
                                        "value": "@activity('Execute Stored Procedure').error.message",
                                        "type": "Expression"
                                    },
                                    "type": "string"
                                }
                            }
                        },
                        "linkedServiceName": {
                            "referenceName": "LSInsightDW",
                            "type": "LinkedServiceReference"
                        }
                    },
                    {
                        "name": "Last-TSE-RowID-Error",
                        "description": "Stored procedure activity to store the new Pipeline run metadata in LSAudit table",
                        "type": "SqlServerStoredProcedure",
                        "dependsOn": [
                            {
                                "activity": "Last-RowID",
                                "dependencyConditions": [
                                    "Failed"
                                ]
                            }
                        ],
                        "policy": {
                            "timeout": "7.00:00:00",
                            "retry": 0,
                            "retryIntervalInSeconds": 30
                        },
                        "userProperties": [],
                        "typeProperties": {
                            "storedProcedureName": "[[dbo].[UpdateLSInsightAudit]",
                            "storedProcedureParameters": {
                                "ADFRunGUID": {
                                    "value": {
                                        "value": "@pipeline().RunId",
                                        "type": "Expression"
                                    },
                                    "type": "String"
                                },
                                "CopyDuration": {
                                    "value": {
                                        "value": "0",
                                        "type": "Expression"
                                    },
                                    "type": "Int32"
                                },
                                "PipelineName": {
                                    "value": {
                                        "value": "@concat('Failed Run - ',  pipeline().Pipeline)",
                                        "type": "Expression"
                                    },
                                    "type": "String"
                                },
                                "PipelineStart": {
                                    "value": {
                                        "value": "@variables('PipelineStart')",
                                        "type": "Expression"
                                    },
                                    "type": "DateTime"
                                },
                                "RowsCopied": {
                                    "value": {
                                        "value": "0",
                                        "type": "Expression"
                                    },
                                    "type": "Int32"
                                },
                                "RowsRead": {
                                    "value": {
                                        "value": "0",
                                        "type": "Expression"
                                    },
                                    "type": "Int32"
                                },
                                "RowVersion": {
                                    "value": {
                                        "value": "0",
                                        "type": "Expression"
                                    },
                                    "type": "Int64"
                                },
                                "Table": {
                                    "value": {
                                        "value": "@variables('DestinationTable')",
                                        "type": "Expression"
                                    },
                                    "type": "String"
                                },
                                "PipelineTriggerType": {
                                    "value": {
                                        "value": "@pipeline().TriggerType",
                                        "type": "Expression"
                                    },
                                    "type": "String"
                                },
                                "ErrorMessage": {
                                    "value": {
                                        "value": "@activity('Last-RowID').output.Error.Message",
                                        "type": "Expression"
                                    },
                                    "type": "string"
                                },
                                "Note": {
                                    "value": "TSE"
                                }
                            }
                        },
                        "linkedServiceName": {
                            "referenceName": "LSInsightDW",
                            "type": "LinkedServiceReference"
                        }
                    },
                    {
                        "name": "Execute Stored Procedure",
                        "type": "Lookup",
                        "dependsOn": [
                            {
                                "activity": "Last-RowID",
                                "dependencyConditions": [
                                    "Succeeded"
                                ]
                            },
                            {
                                "activity": "Current-RowID",
                                "dependencyConditions": [
                                    "Succeeded"
                                ]
                            }
                        ],
                        "policy": {
                            "timeout": "7.00:00:00",
                            "retry": 0,
                            "retryIntervalInSeconds": 30,
                            "secureOutput": false,
                            "secureInput": false
                        },
                        "userProperties": [],
                        "typeProperties": {
                            "source": {
                                "type": "AzureSqlSource",
                                "sqlReaderStoredProcedureName": "[[dbo].[factDiscounts]",
                                "storedProcedureParameters": {
                                    "NewRowID": {
                                        "type": "Int64",
                                        "value": {
                                            "value": "@activity('Current-RowID').output.firstRow.NewRowID",
                                            "type": "Expression"
                                        }
                                    },
                                    "CurrentRowID": {
                                        "type": "Int64",
                                        "value": {
                                            "value": "@activity('Last-RowID').output.firstRow.LastRowID",
                                            "type": "Expression"
                                        }
                                    }
                                },
                                "queryTimeout": "23:00:00",
                                "partitionOption": "None"
                            },
                            "dataset": {
                                "referenceName": "LSInsightDW",
                                "type": "DatasetReference",
                                "parameters": {
                                    "DestTableName": "NA"
                                }
                            }
                        }
                    },
                    {
                        "name": "Current-RowID",
                        "description": "Retrieve  the current maximum value in watermark column of source data store",
                        "type": "Lookup",
                        "dependsOn": [],
                        "policy": {
                            "timeout": "7.00:00:00",
                            "retry": 0,
                            "retryIntervalInSeconds": 30
                        },
                        "userProperties": [],
                        "typeProperties": {
                            "source": {
                                "type": "AzureSqlSource",
                                "sqlReaderQuery": {
                                    "value": "@concat('IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(''',variables('Data_Source_Table_Name'),'''))  select MAX(convert(bigint, ',pipeline().parameters.Data_Source_WaterMarkColumn, ')) as NewRowID from ',variables('Data_Source_Table_Name'), ' ELSE Select 0 AS NewRowID ')",
                                    "type": "Expression"
                                },
                                "partitionOption": "None"
                            },
                            "dataset": {
                                "referenceName": "LSInsightDW",
                                "type": "DatasetReference",
                                "parameters": {
                                    "DestTableName": "NA"
                                }
                            }
                        }
                    },
                    {
                        "name": "Current-RowID-TSE-Error",
                        "description": "Stored procedure activity to store the new Pipeline run metadata in LSAudit table",
                        "type": "SqlServerStoredProcedure",
                        "dependsOn": [
                            {
                                "activity": "Current-RowID",
                                "dependencyConditions": [
                                    "Failed"
                                ]
                            }
                        ],
                        "policy": {
                            "timeout": "7.00:00:00",
                            "retry": 0,
                            "retryIntervalInSeconds": 30
                        },
                        "userProperties": [],
                        "typeProperties": {
                            "storedProcedureName": "[[dbo].[UpdateLSInsightAudit]",
                            "storedProcedureParameters": {
                                "ADFRunGUID": {
                                    "value": {
                                        "value": "@pipeline().RunId",
                                        "type": "Expression"
                                    },
                                    "type": "String"
                                },
                                "CopyDuration": {
                                    "value": {
                                        "value": "0",
                                        "type": "Expression"
                                    },
                                    "type": "Int32"
                                },
                                "PipelineName": {
                                    "value": {
                                        "value": "@concat('Failed Run - ',  pipeline().Pipeline)",
                                        "type": "Expression"
                                    },
                                    "type": "String"
                                },
                                "PipelineStart": {
                                    "value": {
                                        "value": "@variables('PipelineStart')",
                                        "type": "Expression"
                                    },
                                    "type": "DateTime"
                                },
                                "RowsCopied": {
                                    "value": {
                                        "value": "0",
                                        "type": "Expression"
                                    },
                                    "type": "Int32"
                                },
                                "RowsRead": {
                                    "value": {
                                        "value": "0",
                                        "type": "Expression"
                                    },
                                    "type": "Int32"
                                },
                                "RowVersion": {
                                    "value": {
                                        "value": "0",
                                        "type": "Expression"
                                    },
                                    "type": "Int64"
                                },
                                "Table": {
                                    "value": {
                                        "value": "@variables('DestinationTable')",
                                        "type": "Expression"
                                    },
                                    "type": "String"
                                },
                                "PipelineTriggerType": {
                                    "value": {
                                        "value": "@pipeline().TriggerType",
                                        "type": "Expression"
                                    },
                                    "type": "String"
                                },
                                "ErrorMessage": {
                                    "value": {
                                        "value": "@activity('Current-RowID').output.Error.Message",
                                        "type": "Expression"
                                    },
                                    "type": "string"
                                },
                                "Note": {
                                    "value": "TSE"
                                }
                            }
                        },
                        "linkedServiceName": {
                            "referenceName": "LSInsightDW",
                            "type": "LinkedServiceReference"
                        }
                    }
                ],
                "parameters": {
                    "Data_Source_WaterMarkColumn": {
                        "type": "String",
                        "defaultValue": "[[bigint_timestamp]"
                    },
                    "Control_Table_Table_Name": {
                        "type": "String",
                        "defaultValue": "[[dbo].[LSInsightAudit]"
                    },
                    "Control_Table_Column_Name": {
                        "type": "String",
                        "defaultValue": "[[NewRowVersion]"
                    },
                    "TablePrefix": {
                        "type": "string",
                        "defaultValue": "[parameters('TablePrefix')]"
                    }
                },
                "variables": {
                    "PipelineStart": {
                        "type": "String"
                    },
                    "DestinationTable": {
                        "type": "String",
                        "defaultValue": "[[DW].[fDiscounts]"
                    },
                    "Data_Source_Table_Name": {
                        "type": "String",
                        "defaultValue": "[[Stg$Trans_ Discount Entry]"
                    },
                    "ErrorMessage": {
                        "type": "String"
                    }
                },
                "folder": {
                    "name": "4 - Fact Tables"
                },
                "annotations": []
            },
            "dependsOn": [
                "[concat(variables('factoryId'), '/linkedServices/LSInsightDW')]",
                "[concat(variables('factoryId'), '/datasets/LSInsightDW')]"
            ]
        },
        {
            "name": "[concat(parameters('factoryName'), '/PL-SP-prefactItemCostAdjustment')]",
            "type": "Microsoft.DataFactory/factories/pipelines",
            "apiVersion": "2018-06-01",
            "properties": {
                "description": "a SQL select statement to fill the table DW.fItemCostAdjustment using FULL LOAD as this captures the cost adjustments in the Value Entry Table. \n\nThis pipeline will need to run after the staging pipelines and before the fact tables, could be added to the outrigger run batch.",
                "activities": [
                    {
                        "name": "UpdateLSInsightAudit-TSE",
                        "description": "Stored procedure activity to store the new Pipeline run metadata in LSAudit table",
                        "type": "SqlServerStoredProcedure",
                        "dependsOn": [
                            {
                                "activity": "Execute Stored Procedure",
                                "dependencyConditions": [
                                    "Succeeded"
                                ]
                            }
                        ],
                        "policy": {
                            "timeout": "7.00:00:00",
                            "retry": 0,
                            "retryIntervalInSeconds": 30
                        },
                        "userProperties": [],
                        "typeProperties": {
                            "storedProcedureName": "[[dbo].[UpdateLSInsightAudit]",
                            "storedProcedureParameters": {
                                "ADFRunGUID": {
                                    "value": {
                                        "value": "@pipeline().RunId",
                                        "type": "Expression"
                                    },
                                    "type": "String"
                                },
                                "CopyDuration": {
                                    "value": null,
                                    "type": "Int32"
                                },
                                "PipelineName": {
                                    "value": {
                                        "value": "@pipeline().Pipeline",
                                        "type": "Expression"
                                    },
                                    "type": "String"
                                },
                                "PipelineStart": {
                                    "value": {
                                        "value": "@variables('PipelineStart')",
                                        "type": "Expression"
                                    },
                                    "type": "DateTime"
                                },
                                "RowsCopied": {
                                    "value": {
                                        "value": "@activity('Execute Stored Procedure').output.firstRow.RowCount",
                                        "type": "Expression"
                                    },
                                    "type": "Int32"
                                },
                                "RowsRead": {
                                    "value": null,
                                    "type": "Int32"
                                },
                                "RowVersion": {
                                    "value": null,
                                    "type": "Int64"
                                },
                                "Table": {
                                    "value": {
                                        "value": "@variables('DestinationTable')",
                                        "type": "Expression"
                                    },
                                    "type": "String"
                                },
                                "PipelineTriggerType": {
                                    "value": {
                                        "value": "@pipeline().TriggerType",
                                        "type": "Expression"
                                    },
                                    "type": "String"
                                },
                                "ErrorMessage": {
                                    "value": null,
                                    "type": "String"
                                },
                                "Note": {
                                    "value": null
                                }
                            }
                        },
                        "linkedServiceName": {
                            "referenceName": "LSInsightDW",
                            "type": "LinkedServiceReference"
                        }
                    },
                    {
                        "name": "PipelineStart",
                        "type": "SetVariable",
                        "dependsOn": [],
                        "userProperties": [],
                        "typeProperties": {
                            "variableName": "PipelineStart",
                            "value": {
                                "value": "@utcnow()",
                                "type": "Expression"
                            }
                        }
                    },
                    {
                        "name": "SqlServerStoredProcedure1",
                        "description": "Stored procedure activity to store the new Pipeline run metadata in LSAudit table",
                        "type": "SqlServerStoredProcedure",
                        "dependsOn": [
                            {
                                "activity": "Execute Stored Procedure",
                                "dependencyConditions": [
                                    "Failed"
                                ]
                            }
                        ],
                        "policy": {
                            "timeout": "7.00:00:00",
                            "retry": 0,
                            "retryIntervalInSeconds": 30
                        },
                        "userProperties": [],
                        "typeProperties": {
                            "storedProcedureName": "[[dbo].[UpdateLSInsightAudit]",
                            "storedProcedureParameters": {
                                "ADFRunGUID": {
                                    "value": {
                                        "value": "@pipeline().RunId",
                                        "type": "Expression"
                                    },
                                    "type": "String"
                                },
                                "CopyDuration": {
                                    "value": {
                                        "value": "0",
                                        "type": "Expression"
                                    },
                                    "type": "Int32"
                                },
                                "PipelineName": {
                                    "value": {
                                        "value": "@concat('Failed Run - ',  pipeline().Pipeline)",
                                        "type": "Expression"
                                    },
                                    "type": "String"
                                },
                                "PipelineStart": {
                                    "value": {
                                        "value": "@variables('PipelineStart')",
                                        "type": "Expression"
                                    },
                                    "type": "DateTime"
                                },
                                "RowsCopied": {
                                    "value": {
                                        "value": "0",
                                        "type": "Expression"
                                    },
                                    "type": "Int32"
                                },
                                "RowsRead": {
                                    "value": {
                                        "value": "0",
                                        "type": "Expression"
                                    },
                                    "type": "Int32"
                                },
                                "RowVersion": {
                                    "value": {
                                        "value": "0",
                                        "type": "Expression"
                                    },
                                    "type": "Int64"
                                },
                                "Table": {
                                    "value": {
                                        "value": "@variables('Data_Source_Table_Name')",
                                        "type": "Expression"
                                    },
                                    "type": "String"
                                },
                                "PipelineTriggerType": {
                                    "value": {
                                        "value": "@pipeline().TriggerType",
                                        "type": "Expression"
                                    },
                                    "type": "String"
                                },
                                "ErrorMessage": {
                                    "value": {
                                        "value": "@activity('Execute Stored Procedure').error.message",
                                        "type": "Expression"
                                    },
                                    "type": "string"
                                }
                            }
                        },
                        "linkedServiceName": {
                            "referenceName": "LSInsightDW",
                            "type": "LinkedServiceReference"
                        }
                    },
                    {
                        "name": "Execute Stored Procedure",
                        "type": "Lookup",
                        "dependsOn": [
                            {
                                "activity": "PipelineStart",
                                "dependencyConditions": [
                                    "Succeeded"
                                ]
                            }
                        ],
                        "policy": {
                            "timeout": "7.00:00:00",
                            "retry": 0,
                            "retryIntervalInSeconds": 30,
                            "secureOutput": false,
                            "secureInput": false
                        },
                        "userProperties": [],
                        "typeProperties": {
                            "source": {
                                "type": "AzureSqlSource",
                                "sqlReaderStoredProcedureName": "[[dbo].[prefactItemCostAdjustment]",
                                 "queryTimeout": "23:00:00",
                                "partitionOption": "None"
                            },
                            "dataset": {
                                "referenceName": "LSInsightDW",
                                "type": "DatasetReference",
                                "parameters": {
                                    "DestTableName": "NA"
                                }
                            }
                        }
                    }
                ],
                "parameters": {
                    "Data_Source_Table_Name": {
                        "type": "String",
                        "defaultValue": "[[Stg$Value Entry]"
                    },
                    "Data_Source_WaterMarkColumn": {
                        "type": "String",
                        "defaultValue": "[[bigint_timestamp]"
                    },
                    "Control_Table_Table_Name": {
                        "type": "String",
                        "defaultValue": "[[dbo].[LSInsightAudit]"
                    },
                    "Control_Table_Column_Name": {
                        "type": "String",
                        "defaultValue": "[[NewRowVersion]"
                    },
                    "TablePrefix": {
                        "type": "string",
                        "defaultValue": "[parameters('TablePrefix')]"
                    }
                },
                "variables": {
                    "PipelineStart": {
                        "type": "String"
                    },
                    "DestinationTable": {
                        "type": "String",
                        "defaultValue": "[[DW].[fItemCostAdjustment]"
                    },
                    "Data_Source_Table_Name": {
                        "type": "String"
                    },
                    "ErrorMessage": {
                        "type": "String"
                    }
                },
                "folder": {
                    "name": "4 - Fact Tables"
                },
                "annotations": []
            },
            "dependsOn": [
                "[concat(variables('factoryId'), '/linkedServices/LSInsightDW')]",
                "[concat(variables('factoryId'), '/datasets/LSInsightDW')]"  
            ]
        },

        {
            "name": "[concat(parameters('factoryName'), '/PL-SP-postfactUpdateAdjustedCost')]",
            "type": "Microsoft.DataFactory/factories/pipelines",
            "apiVersion": "2018-06-01",
            "properties": {
                "description": "Execute Stored Procedure in LS Insight DW",
                "activities": [
                    {
                        "name": "UpdateLSInsightAudit-TSE",
                        "description": "Stored procedure activity to store the new Pipeline run metadata in LSAudit table",
                        "type": "SqlServerStoredProcedure",
                        "dependsOn": [
                            {
                                "activity": "Execute Stored Procedure",
                                "dependencyConditions": [
                                    "Succeeded"
                                ]
                            }
                        ],
                        "policy": {
                            "timeout": "7.00:00:00",
                            "retry": 0,
                            "retryIntervalInSeconds": 30
                        },
                        "userProperties": [],
                        "typeProperties": {
                            "storedProcedureName": "[[dbo].[UpdateLSInsightAudit]",
                            "storedProcedureParameters": {
                                "ADFRunGUID": {
                                    "value": {
                                        "value": "@pipeline().RunId",
                                        "type": "Expression"
                                    },
                                    "type": "String"
                                },
                                "CopyDuration": {
                                    "value": null,
                                    "type": "Int32"
                                },
                                "PipelineName": {
                                    "value": {
                                        "value": "@pipeline().Pipeline",
                                        "type": "Expression"
                                    },
                                    "type": "String"
                                },
                                "PipelineStart": {
                                    "value": {
                                        "value": "@variables('PipelineStart')",
                                        "type": "Expression"
                                    },
                                    "type": "DateTime"
                                },
                                "RowsCopied": {
                                    "value": {
                                        "value": "@activity('Execute Stored Procedure').output.firstRow.RowCount",
                                        "type": "Expression"
                                    },
                                    "type": "Int32"
                                },
                                "RowsRead": {
                                    "value": null,
                                    "type": "Int32"
                                },
                                "RowVersion": {
                                    "value": null,
                                    "type": "Int64"
                                },
                                "Table": {
                                    "value": {
                                        "value": "@variables('DestinationTable')",
                                        "type": "Expression"
                                    },
                                    "type": "String"
                                },
                                "PipelineTriggerType": {
                                    "value": {
                                        "value": "@pipeline().TriggerType",
                                        "type": "Expression"
                                    },
                                    "type": "String"
                                },
                                "ErrorMessage": {
                                    "value": null,
                                    "type": "String"
                                },
                                "Note": {
                                    "value": null
                                }
                            }
                        },
                        "linkedServiceName": {
                            "referenceName": "LSInsightDW",
                            "type": "LinkedServiceReference"
                        }
                    },
                    {
                        "name": "PipelineStart",
                        "type": "SetVariable",
                        "dependsOn": [],
                        "userProperties": [],
                        "typeProperties": {
                            "variableName": "PipelineStart",
                            "value": {
                                "value": "@utcnow()",
                                "type": "Expression"
                            }
                        }
                    },
                    {
                        "name": "SqlServerStoredProcedure1",
                        "description": "Stored procedure activity to store the new Pipeline run metadata in LSAudit table",
                        "type": "SqlServerStoredProcedure",
                        "dependsOn": [
                            {
                                "activity": "Execute Stored Procedure",
                                "dependencyConditions": [
                                    "Failed"
                                ]
                            }
                        ],
                        "policy": {
                            "timeout": "7.00:00:00",
                            "retry": 0,
                            "retryIntervalInSeconds": 30
                        },
                        "userProperties": [],
                        "typeProperties": {
                            "storedProcedureName": "[[dbo].[UpdateLSInsightAudit]",
                            "storedProcedureParameters": {
                                "ADFRunGUID": {
                                    "value": {
                                        "value": "@pipeline().RunId",
                                        "type": "Expression"
                                    },
                                    "type": "String"
                                },
                                "CopyDuration": {
                                    "value": {
                                        "value": "0",
                                        "type": "Expression"
                                    },
                                    "type": "Int32"
                                },
                                "PipelineName": {
                                    "value": {
                                        "value": "@concat('Failed Run - ',  pipeline().Pipeline)",
                                        "type": "Expression"
                                    },
                                    "type": "String"
                                },
                                "PipelineStart": {
                                    "value": {
                                        "value": "@variables('PipelineStart')",
                                        "type": "Expression"
                                    },
                                    "type": "DateTime"
                                },
                                "RowsCopied": {
                                    "value": {
                                        "value": "0",
                                        "type": "Expression"
                                    },
                                    "type": "Int32"
                                },
                                "RowsRead": {
                                    "value": {
                                        "value": "0",
                                        "type": "Expression"
                                    },
                                    "type": "Int32"
                                },
                                "RowVersion": {
                                    "value": {
                                        "value": "0",
                                        "type": "Expression"
                                    },
                                    "type": "Int64"
                                },
                                "Table": {
                                    "value": {
                                        "value": "@variables('Data_Source_Table_Name')",
                                        "type": "Expression"
                                    },
                                    "type": "String"
                                },
                                "PipelineTriggerType": {
                                    "value": {
                                        "value": "@pipeline().TriggerType",
                                        "type": "Expression"
                                    },
                                    "type": "String"
                                },
                                "ErrorMessage": {
                                    "value": {
                                        "value": "@activity('Execute Stored Procedure').error.message",
                                        "type": "Expression"
                                    },
                                    "type": "string"
                                }
                            }
                        },
                        "linkedServiceName": {
                            "referenceName": "LSInsightDW",
                            "type": "LinkedServiceReference"
                        }
                    },
                    {
                        "name": "Execute Stored Procedure",
                        "type": "Lookup",
                        "dependsOn": [
                            {
                                "activity": "PipelineStart",
                                "dependencyConditions": [
                                    "Succeeded"
                                ]
                            }
                        ],
                        "policy": {
                            "timeout": "7.00:00:00",
                            "retry": 0,
                            "retryIntervalInSeconds": 30,
                            "secureOutput": false,
                            "secureInput": false
                        },
                        "userProperties": [],
                        "typeProperties": {
                            "source": {
                                "type": "AzureSqlSource",
                                "sqlReaderStoredProcedureName": "[[dbo].[postfactUpdateAdjustedCost]",
                                "queryTimeout": "23:00:00",
                                "partitionOption": "None"
                            },
                            "dataset": {
                                "referenceName": "LSInsightDW",
                                "type": "DatasetReference",
                                "parameters": {
                                    "DestTableName": "NA"
                                }
                            }
                        }
                    }
                ],
                "parameters": {
                    "Data_Source_WaterMarkColumn": {
                        "type": "String",
                        "defaultValue": "[[RowID]"
                    },
                    "Control_Table_Table_Name": {
                        "type": "String",
                        "defaultValue": "[[dbo].[LSInsightAudit]"
                    },
                    "Control_Table_Column_Name": {
                        "type": "String",
                        "defaultValue": "[[NewRowVersion]"
                    },
                    "TablePrefix": {
                        "type": "string",
                        "defaultValue": "[parameters('TablePrefix')]"
                    }
                },
                "variables": {
                    "PipelineStart": {
                        "type": "String"
                    },
                    "DestinationTable": {
                        "type": "String",
                        "defaultValue": "[[DW].[FactSalesPosted]"
                    },
                    "Data_Source_Table_Name": {
                        "type": "String",
                        "defaultValue": "[[Stg$Item Cost Adjustment]"
                    },
                    "ErrorMessage": {
                        "type": "String"
                    }
                },
                "folder": {
                    "name": "4 - Fact Tables"
                },
                "annotations": []
            },
            "dependsOn": [
                "[concat(variables('factoryId'), '/linkedServices/LSInsightDW')]",
                "[concat(variables('factoryId'), '/datasets/LSInsightDW')]" 
            ]
        }
        ,
        {
            "name": "[concat(parameters('factoryName'), '/Scheduled Run')]",
            "type": "Microsoft.DataFactory/factories/pipelines",
            "apiVersion": "2018-06-01",
            "properties": {
            "description": "This will run the daily data update from LS Central to staging to Dimension and Facts in the LS Insight Data Warehouse",
            "activities":  [
            {
                "name": "Extract from Source",
                "description": "Extract data from source tables in the LS Central database",
                "type": "ExecutePipeline",
                "dependsOn": [],
                "userProperties": [],
                "typeProperties": {
                    "pipeline": {
                        "referenceName": "All staging Tables",
                        "type": "PipelineReference"
                    },
                    "waitOnCompletion": true
                }
            },
            {
                "name": "Clean dimensions and Load",
                "description": "Clean data from the staging tables and Load to Dimensions in the star schema model.",
                "type": "ExecutePipeline",
                "dependsOn": [
                    {
                        "activity": "Extract from Source",
                        "dependencyConditions": [
                            "Succeeded"
                        ]
                    }
                ],
                "userProperties": [],
                "typeProperties": {
                    "pipeline": {
                        "referenceName": "Execute Dimension Stored Procedures",
                        "type": "PipelineReference"
                    },
                    "waitOnCompletion": true
                }
            },
            {
                "name": "Outrigger Dimension Member Attribute",
                "description": "Build the Outrigger Dimension Member Attribute",
                "type": "ExecutePipeline",
                "dependsOn": [
                    {
                        "activity": "Clean dimensions and Load",
                        "dependencyConditions": [
                            "Succeeded"
                        ]
                    }
                ],
                "userProperties": [],
                "typeProperties": {
                    "pipeline": {
                        "referenceName": "PL-SP-odMemberAttributes",
                        "type": "PipelineReference"
                    },
                    "waitOnCompletion": true
                }
            },
            {
                "name": "Execute FactSalesPosted",
                "type": "ExecutePipeline",
                "dependsOn": [
                    {
                        "activity": "Execute ItemCostAdjustment",
                        "dependencyConditions": [
                            "Succeeded"
                        ]
                    }
                ],
                "userProperties": [],
                "typeProperties": {
                    "pipeline": {
                        "referenceName": "PL-SP-FactSalesPosted",
                        "type": "PipelineReference"
                    },
                    "waitOnCompletion": true
                }
            },
            {
                "name": "Execute FactDiscount",
                "type": "ExecutePipeline",
                "dependsOn": [
                    {
                        "activity": "Execute FactSalesPosted",
                        "dependencyConditions": [
                            "Succeeded"
                        ]
                    }
                ],
                "userProperties": [],
                "typeProperties": {
                    "pipeline": {
                        "referenceName": "PL-SP-factDiscount",
                        "type": "PipelineReference"
                    },
                    "waitOnCompletion": true
                }
            },
            {
                "name": "Execute FactInventory",
                "type": "ExecutePipeline",
                "dependsOn": [
                    {
                        "activity": "Execute ItemCostAdjustment",
                        "dependencyConditions": [
                            "Succeeded"
                        ]
                    }
                ],
                "userProperties": [],
                "typeProperties": {
                    "pipeline": {
                        "referenceName": "PL-SP-factInventory",
                        "type": "PipelineReference"
                    },
                    "waitOnCompletion": true
                }
            },

            {
                "name": "PL-SP-factACIAlerts",
                "type": "ExecutePipeline",
                "dependsOn": [
                    {
                        "activity": "Execute ItemCostAdjustment",
                        "dependencyConditions": [
                            "Succeeded"
                        ]
                    }
                ],
                "userProperties": [],
                "typeProperties": {
                    "pipeline": {
                        "referenceName": "PL-SP-factACIAlerts",
                        "type": "PipelineReference"
                    },
                    "waitOnCompletion": true
                }
            },
            {
                "name": "PL-SP-factACISignals",
                "type": "ExecutePipeline",
                "dependsOn": [
                    {
                        "activity": "Execute ItemCostAdjustment",
                        "dependencyConditions": [
                            "Succeeded"
                        ]
                    }
                ],
                "userProperties": [],
                "typeProperties": {
                    "pipeline": {
                        "referenceName": "PL-SP-factACISignals",
                        "type": "PipelineReference"
                    },
                    "waitOnCompletion": true
                }
            },
            {
                "name": "Execute ItemCostAdjustment",
                "type": "ExecutePipeline",
                "dependsOn": [
                    {
                        "activity": "Outrigger Dimension Member Attribute",
                        "dependencyConditions": [
                            "Succeeded"
                        ]
                    }
                ],
                "userProperties": [],
                "typeProperties": {
                    "pipeline": {
                        "referenceName": "PL-SP-prefactItemCostAdjustment",
                        "type": "PipelineReference"
                    },
                    "waitOnCompletion": true
                }
            },
            {
                "name": "Execute UpdateAdjustedCost",
                "type": "ExecutePipeline",
                "dependsOn": [
                    {
                        "activity": "Execute FactSalesPosted",
                        "dependencyConditions": [
                            "Succeeded"
                        ]
                    }
                ],
                "userProperties": [],
                "typeProperties": {
                    "pipeline": {
                        "referenceName": "PL-SP-postfactUpdateAdjustedCost",
                        "type": "PipelineReference"
                    },
                    "waitOnCompletion": true
                }
            },
            {
                "name": "Check if Hotel Extension Exists",
                "type": "GetMetadata",
                "dependsOn": [
                    {
                        "activity": "Extract from Source",
                        "dependencyConditions": [
                            "Succeeded"
                        ]
                    }
                ],
                "policy": {
                    "timeout": "7.00:00:00",
                    "retry": 0,
                    "retryIntervalInSeconds": 30,
                    "secureOutput": false,
                    "secureInput": false
                },
                "userProperties": [],
                "typeProperties": {
                    "dataset": {
                        "referenceName": "LSInsightDW",
                        "type": "DatasetReference",
                        "parameters": {
                            "DestTableName": "stg$Detailed Revenue Entry"
                        }
                    },
                    "fieldList": [
                        "exists"
                    ]
                }
            },
            {
                "name": "If Condition1",
                "type": "IfCondition",
                "dependsOn": [
                    {
                        "activity": "Check if Hotel Extension Exists",
                        "dependencyConditions": [
                            "Succeeded"
                        ]
                    },
                    {
                        "activity": "Execute ItemCostAdjustment",
                        "dependencyConditions": [
                            "Succeeded"
                        ]
                    }
                ],
                "userProperties": [],
                "typeProperties": {
                    "expression": {
                        "value": "@activity('Check if Hotel Extension Exists').output.Exists",
                        "type": "Expression"
                    },
                    "ifTrueActivities": [
                        {
                            "name": "Execute fHotelDetailedRevenue",
                            "type": "ExecutePipeline",
                            "dependsOn": [],
                            "userProperties": [],
                            "typeProperties": {
                                "pipeline": {
                                    "referenceName": "PL-SP-factHotelDetailedRevenue",
                                    "type": "PipelineReference"
                                },
                                "waitOnCompletion": true
                            }
                        },
                        {
                            "name": "Execute fHotelStatistic",
                            "type": "ExecutePipeline",
                            "dependsOn": [],
                            "userProperties": [],
                            "typeProperties": {
                                "pipeline": {
                                    "referenceName": "PL-SP-factHotelStatistic",
                                    "type": "PipelineReference"
                                },
                                "waitOnCompletion": true
                            }
                        }
                    ]
                }
                 },

                {
                "name": "PL-SP-factPurchaseInvoice",
                "type": "ExecutePipeline",
                "dependsOn": [
                    {
                        "activity": "Execute ItemCostAdjustment",
                        "dependencyConditions": [
                            "Succeeded"
                        ]
                    }
                ],
                "userProperties": [],
                "typeProperties": {
                    "pipeline": {
                        "referenceName": "PL-SP-factPurchaseInvoice",
                        "type": "PipelineReference"
                    },
                    "waitOnCompletion": true
                }
            },
            {
                "name": "PL-SP-factPurchaseCreditMemo",
                "type": "ExecutePipeline",
                "dependsOn": [
                    {
                        "activity": "Execute ItemCostAdjustment",
                        "dependencyConditions": [
                            "Succeeded"
                        ]
                    }
                ],
                "userProperties": [],
                "typeProperties": {
                    "pipeline": {
                        "referenceName": "PL-SP-factPurchaseCreditMemo",
                        "type": "PipelineReference"
                    },
                    "waitOnCompletion": true
                }
            },
            {
                "name": "PL-SP-factPurchaseOrders",
                "type": "ExecutePipeline",
                "dependsOn": [
                    {
                        "activity": "Execute ItemCostAdjustment",
                        "dependencyConditions": [
                            "Succeeded"
                        ]
                    }
                ],
                "userProperties": [],
                "typeProperties": {
                    "pipeline": {
                        "referenceName": "PL-SP-factPurchaseOrders",
                        "type": "PipelineReference"
                    },
                    "waitOnCompletion": true
                }
            },
            {
                "name": "PL-SP-factPurchaseReceipts",
                "type": "ExecutePipeline",
                "dependsOn": [
                    {
                        "activity": "Execute ItemCostAdjustment",
                        "dependencyConditions": [
                            "Succeeded"
                        ]
                    }
                ],
                "userProperties": [],
                "typeProperties": {
                    "pipeline": {
                        "referenceName": "PL-SP-factPurchaseReceipts",
                        "type": "PipelineReference"
                    },
                    "waitOnCompletion": true
                }
            },

            {
                "name": "PostPipelineFinish",
                "type": "WebActivity",
                "dependsOn": [
                    {
                        "activity": "Execute FactDiscount",
                        "dependencyConditions": [
                            "Succeeded"
                        ]
                    },
                    {
                        "activity": "Execute UpdateAdjustedCost",
                        "dependencyConditions": [
                            "Succeeded"
                        ]
                    },
                    {
                        "activity": "Execute FactInventory",
                        "dependencyConditions": [
                            "Succeeded"
                        ]
                    },
                    {
                        "activity": "If Condition1",
                        "dependencyConditions": [
                            "Completed"
                        ]
                    },
                    {
                        "activity": "GetLSInsightInfo",
                        "dependencyConditions": [
                            "Succeeded"
                        ]
                    }
                ],
                "policy": {
                    "timeout": "7.00:00:00",
                    "retry": 0,
                    "retryIntervalInSeconds": 30,
                    "secureOutput": false,
                    "secureInput": false
                },
                "userProperties": [],
                "typeProperties": {
                    "url": "https://lsinsighttelemetry.lsretail.com/PipelineService",
                    "method": "POST",
                    "body": {
                        "value": "@concat('{\n  \"serverName\": \"',activity('GetLSInsightInfo').output.firstrow.ServerName,'\",\n  \"databaseName\": \"',activity('GetLSInsightInfo').output.firstrow.DatabaseName,'\",\n  \"insightVersion\": \"',activity('GetLSInsightInfo').output.firstrow.VersionNo,'\"\n}')",
                        "type": "Expression"
                    }
                }
            },
            {
                "name": "GetLSInsightInfo",
                "type": "Lookup",
                "dependsOn": [],
                "policy": {
                    "timeout": "7.00:00:00",
                    "retry": 0,
                    "retryIntervalInSeconds": 30,
                    "secureOutput": false,
                    "secureInput": false
                },
                "userProperties": [],
                "typeProperties": {
                    "source": {
                        "type": "AzureSqlSource",
                        "sqlReaderQuery": "SELECT [VersionNo]\n,DB_NAME() AS 'DatabaseName'\n,\nCASE WHEN  CONVERT(sysname, SERVERPROPERTY('Edition')) LIKE '%Azure%' \nTHEN\nCONCAT (\nCONVERT(NVARCHAR(100), SERVERPROPERTY('ServerName'))\n,'.database.windows.net'\n)\nELSE \nCONVERT(NVARCHAR(100), SERVERPROPERTY('ServerName'))\nEND\nAS 'ServerName'\nFROM [dbo].[LSInsightVersion]",
                        "queryTimeout": "02:00:00",
                        "partitionOption": "None"
                    },
                    "dataset": {
                        "referenceName": "LSCentralColumnMetadata",
                        "type": "DatasetReference"
                    }
                }
            }
        ],
                "folder": {
                    "name": "5 - Pipeline Run Diagrams"
                },
                "annotations": []
                },
            "dependsOn": [
                "[concat(variables('factoryId'), '/linkedServices/LSInsightDW')]",
                "[concat(variables('factoryId'), '/pipelines/All staging Tables')]",
                "[concat(variables('factoryId'), '/pipelines/Execute Dimension Stored Procedures')]",
                "[concat(variables('factoryId'), '/pipelines/PL-SP-odMemberAttributes')]",
                "[concat(variables('factoryId'), '/pipelines/PL-SP-FactSalesPosted')]",
                "[concat(variables('factoryId'), '/pipelines/PL-SP-factDiscount')]",
                "[concat(variables('factoryId'), '/pipelines/PL-SP-factInventory')]",
                "[concat(variables('factoryId'), '/pipelines/PL-SP-prefactItemCostAdjustment')]",
                "[concat(variables('factoryId'), '/pipelines/PL-SP-postfactupdateadjustedcost')]",
                "[concat(variables('factoryId'), '/pipelines/pl-sp-facthoteldetailedrevenue')]",
                "[concat(variables('factoryId'), '/pipelines/PL-SP-factACIAlerts')]",
                "[concat(variables('factoryId'), '/pipelines/PL-SP-factACISignals')]",
                "[concat(variables('factoryId'), '/pipelines/PL-SP-factHotelStatistic')]",
                "[concat(variables('factoryId'), '/datasets/LSInsightDW')]",
                "[concat(variables('factoryId'), '/pipelines/pl-sp-factpurchaseinvoice')]",
                "[concat(variables('factoryId'), '/pipelines/pl-sp-factpurchaseCreditMemo')]",
                "[concat(variables('factoryId'), '/pipelines/pl-sp-factpurchaseOrders')]",
                "[concat(variables('factoryId'), '/pipelines/pl-sp-factpurchaseReceipts')]",
                "[concat(variables('factoryId'), '/datasets/LSCentralColumnMetadata')]"

            ]
        },
        {
            "name": "[concat(parameters('factoryName'), '/Initial load')]",
            "type": "Microsoft.DataFactory/factories/pipelines",
            "apiVersion": "2018-06-01",
            "properties": {
            "description": "Full load of ALL source data metadata and generation of query base and initial data load",
            "activities":  [
            {
                "name": "Populate LS Insight Query Base",
                "description": "Full load of ALL source data metadata and generation of query base",
                "type": "ExecutePipeline",
                "dependsOn": [],
                "userProperties": [],
                "typeProperties": {
                    "pipeline": {
                        "referenceName": "PopulateQueryBase",
                        "type": "PipelineReference"
                    },
                    "waitOnCompletion": true,
                    "parameters": {
                        "PopulateQueryBase": true,
                        "GetLSCentralMetadata": true
                    }
                }
            },
            {
                "name": "Initial load",
                "type": "ExecutePipeline",
                "dependsOn": [
                    {
                        "activity": "Populate LS Insight Query Base",
                        "dependencyConditions": [
                            "Succeeded"
                        ]
                    }
                ],
                "userProperties": [],
                "typeProperties": {
                    "pipeline": {
                        "referenceName": "Scheduled Run",
                        "type": "PipelineReference"
                    },
                    "waitOnCompletion": true
                }
            }
        ],
                "folder": {
                    "name": "5 - Pipeline Run Diagrams"
                },
                "concurrency": 1,
                "annotations": []
            },
            "dependsOn": [
                "[concat(variables('factoryId'), '/linkedServices/LSInsightDW')]",
                "[concat(variables('factoryId'), '/pipelines/Scheduled Run')]",
                "[concat(variables('factoryId'), '/pipelines/PopulateQueryBase')]",
                "[concat(variables('factoryId'), '/datasets/LSInsightDW')]"
            ]
        },
        {
            "name": "[concat(parameters('factoryName'), '/Factory reset')]",
            "type": "Microsoft.DataFactory/factories/pipelines",
            "apiVersion": "2018-06-01",
            "properties": {
            "description": "Clearing all tables in teh Data warehouse and full load of ALL source data metadata and generation of query base and initial data load",
            "activities":  [
            {
                "name": "Reset LS Insight DW",
                "description": "Clean up the DW tables",
                "type": "ExecutePipeline",
                "dependsOn": [],
                "userProperties": [],
                "typeProperties": {
                    "pipeline": {
                        "referenceName": "Reset LS Insight DW",
                        "type": "PipelineReference"
                    },
                    "waitOnCompletion": true,
                    "parameters": {
                        "Clean_ALL_DW_Tables": "TRUE"
                    }
                }
            },
            {
                "name": "Initial load",
                "type": "ExecutePipeline",
                "dependsOn": [
                    {
                        "activity": "Reset LS Insight DW",
                        "dependencyConditions": [
                            "Succeeded"
                        ]
                    }
                ],
                "userProperties": [],
                "typeProperties": {
                    "pipeline": {
                        "referenceName": "Initial load",
                        "type": "PipelineReference"
                    },
                    "waitOnCompletion": true
                }
            }
        ],
                "folder": {
                    "name": "5 - Pipeline Run Diagrams"
                },
                "annotations": []
            },
            "dependsOn": [
                "[concat(variables('factoryId'), '/linkedServices/LSInsightDW')]",
                "[concat(variables('factoryId'), '/pipelines/PopulateQueryBase')]",
                "[concat(variables('factoryId'), '/pipelines/Initial load')]",
                "[concat(variables('factoryId'), '/pipelines/Reset LS Insight DW')]",
                "[concat(variables('factoryId'), '/datasets/LSInsightDW')]"
            ]
        }
    ]
}